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: Cache, 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



70
71
72
73
74
75
76
77
78
# File 'lib/rack_page_caching/rack_page_caching.rb', line 70

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 => Rack::PageCaching::Middleware::Cache.adapter.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)


80
81
82
83
84
85
86
# File 'lib/rack_page_caching/rack_page_caching.rb', line 80

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

.generate_key(url) ⇒ Object



98
99
100
# File 'lib/rack_page_caching/rack_page_caching.rb', line 98

def generate_key(url)
  Digest::SHA1.hexdigest(url)
end

.set_into_cache(url, body, content_type, expiry = Rack::PageCaching::Middleware::Cache.adapter.default_expiry) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/rack_page_caching/rack_page_caching.rb', line 88

def set_into_cache(url, body, content_type, expiry = Rack::PageCaching::Middleware::Cache.adapter.default_expiry)
  begin
    Rack::PageCaching::Middleware::Cache.set(Rack::PageCaching::Middleware.generate_key(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 = Rack::PageCaching::Middleware::Cache.get(Rack::PageCaching::Middleware.generate_key(request.url))
        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.url, res.body, res["Content-Type"], cache_control[:max_age])
      end
    end
    return ret
  end
  return @app.call(env)
end