Class: Rack::PageCaching::Middleware

Inherits:
Object
  • Object
show all
Extended by:
HoptoadNotifier::Catcher
Includes:
HoptoadNotifier::Catcher
Defined in:
lib/rack_page_caching/rack_page_caching.rb

Defined Under Namespace

Classes: Page

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

:nodoc:



6
7
8
# File 'lib/rack_page_caching/rack_page_caching.rb', line 6

def initialize(app) # :nodoc:
  @app = app
end

Class Method Details

.cache_controls(headers) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/rack_page_caching/rack_page_caching.rb', line 67

def cache_controls(headers)
  cc_header = headers['Cache-Control'] || 'private'
  cache_control = {:public => (!cc_header.match(/public/).nil? && 
                               cc_header.match(/private/).nil?), 
                   :max_age => configatron.cachetastic_caches_page_cache_options.default_expiry}
  cc_header.match(/max-age=(\d+)/)
  cache_control[:max_age] = $1 unless $1.nil?
  cache_control
end

.cacheable?(request, status, headers) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/rack_page_caching/rack_page_caching.rb', line 77

def cacheable?(request, status, headers)
  if configatron.rack.caching.use_page_caching
    cache_control = Rack::PageCaching::Middleware.cache_controls(headers)
    return cache_control[:public] && request.get? && (status.to_i >= 200 && status.to_i < 300)
  end
  return false
end

.set_into_cache(url, body, content_type, expiry = configatron.cachetastic_caches_page_cache_options.default_expiry) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/rack_page_caching/rack_page_caching.rb', line 85

def set_into_cache(url, body, content_type, expiry = configatron.cachetastic_caches_page_cache_options.default_expiry)
  begin
    Cachetastic::Caches::PageCache.set(url, 
                                       Rack::PageCaching::Middleware::Page.new(body, content_type),                                        
                                       expiry.to_i)
  rescue Exception => e
    notify_hoptoad(e)
  end
end

Instance Method Details

#call(env) ⇒ Object

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack_page_caching/rack_page_caching.rb', line 10

def call(env) # :nodoc:
  if configatron.rack.caching.use_page_caching
    request = Rack::Request.new(env)
    if request.get?
      begin
        page = Cachetastic::Caches::PageCache.get(request.fullpath)
        if page
          response = Rack::Response.new
          response["Content-Type"] = page.content_type
          response.write(page.body)
          return response.finish
        end
      rescue Exception => e
        HoptoadNotifier::Catcher.notify_hoptoad(e)
      end

    end
    ret = @app.call(env)
    unless ret[2].is_a?(Rack::File)
      res = ret[2]
      cache_control = Rack::PageCaching::Middleware.cache_controls(res)
      if Rack::PageCaching::Middleware.cacheable?(request, res.status, res)
        Rack::PageCaching::Middleware.set_into_cache(request.fullpath, res.body, res["Content-Type"], cache_control[:max_age])
      end
    end
    return ret
  end
  return @app.call(env)
end