Class: SuperCache::SimpleFilter

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SimpleFilter

Returns a new instance of SimpleFilter.



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

def initialize(options={})
  
end

Instance Method Details

#filter(controller) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/super_cache/simple_filter.rb', line 7

def filter(controller)
  return yield unless controller.perform_caching
  @cache_path = controller.instance_variable_get('@caches_path') || weird_cache_path(controller)
  request = controller.request
  response = controller.response
  headers = response.headers

  if content = Rails.cache.read(@cache_path, :raw => true)
    return yield if content.size <= 0
    Rails.logger.debug "Hit #{@cache_path}"
    headers['Content-Length'] ||= content.size.to_s
    headers['Content-Type'] ||= request.format.to_s.strip unless  request.format == :all
    controller.send :render, :text => content, :content_type => 'text/html'
    return false
  else
    yield
    body = response.body.to_s
    return if controller.instance_variable_get('@no_cache') || body.size == 0 || response.status.to_i != 200        
    @expires_in = controller.instance_variable_get('@expires_in') || 600 
    Rails.logger.debug("Write #{@cache_path}")
    Rails.cache.write(@cache_path, body, :raw => true, :expires_in => @expires_in.to_i)            
  end
rescue ArgumentError => e
  @no_cache = true
  Rails.logger.info e.to_s
  Rails.logger.debug {e.backtrace}
end