Class: SuperCache::DogPileFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/super_cache/dog_pile_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DogPileFilter

Returns a new instance of DogPileFilter.



4
5
6
# File 'lib/super_cache/dog_pile_filter.rb', line 4

def initialize(options={})
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/super_cache/dog_pile_filter.rb', line 3

def options
  @options
end

Instance Method Details

#filter(controller, action = nil, &block) ⇒ Object

  1. check the expiration info of target key

  2. if target key is expired, add a lock against target key

2.1 if not expired, return the cache 3.1 if lock successfully, then continue to action 3.2 if lock failed, then obtain target key 3.2.1 if target key is missing, then 3.2.1.1 wait for N sec to check if the cache is generated 3.2.1.1.1 if timeout, go to action 3.2.2 target key is not missing, return the cache 3.3 go to the action and obtain response body 3.4 store the response body to the target key and set the expiration with 2x longer 3.5 store the expireation info of target key



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/super_cache/dog_pile_filter.rb', line 19

def filter(controller, action=nil, &block)
  action ||= block
  return action.call unless controller.perform_caching
  options = self.options.dup

  options[:controller] = controller
  options[:action] = action || block
  options[:cache_path] ||= weird_cache_path(options)
  options[:flag_key] = "expires_at:#{options[:cache_path]}"
  options[:expires_in] ||= 600
  options[:content] = nil

  if Rails.cache.read(options[:flag_key], :raw => true) 
    check_cache(options)
  else
    cache_expired(options)
  end
end