Module: Padrino::Cache::Helpers::Page

Defined in:
padrino-cache/lib/padrino-cache/helpers/page.rb

Overview

Page caching is easy to integrate into your application. To turn it on, simply provide the :cache => true option on either a controller or one of its routes. By default, cached content is persisted with a “file store” –that is, in a subdirectory of your application root.

You can manually expire cache with CachedApp.cache.delete(:my_name)

Note that the “latest” method call to expires determines its value: if called within a route, as opposed to a controller definition, the route's value will be assumed.

Examples:

# Setting content expiry time.
class CachedApp < Padrino::Application
  enable :caching          # turns on caching mechanism

  controller '/blog', :cache => true do
    expires 15

    get '/entries' do
      # expires 15 => can also be defined inside a single route
      'Just broke up eating twinkies, lol'
    end

    get '/post/:id' do
      cache_key :my_name
      @post = Post.find(params[:id])
    end
  end
end

Defined Under Namespace

Modules: ClassMethods

Constant Summary

CACHED_VERBS =
{ 'GET' => true, 'HEAD' => true }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.padrino_route_added(route, verb) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'padrino-cache/lib/padrino-cache/helpers/page.rb', line 97

def self.padrino_route_added(route, verb, *)
  return unless route.cache && CACHED_VERBS[verb]

  route.before_filters do
    next unless settings.caching?
    if cached_response = load_cached_response
      content_type cached_response[:content_type]
      halt 200, cached_response[:body]
    end
  end

  route.after_filters do
    save_cached_response(route.cache_expires) if settings.caching?
  end
end

Instance Method Details

#cache_key(name = nil, &block) ⇒ Object

This helper is used within a route or route to indicate the name in the cache.

end

Examples:

controller '/blog', :cache => true do

  get '/post/:id' do
    cache_key :my_name
    @post = Post.find(params[:id])
  end
end
get '/foo', :cache => true do
  cache_key { param[:id] }
  "My id is #{param[:id}"
end


90
91
92
93
# File 'padrino-cache/lib/padrino-cache/helpers/page.rb', line 90

def cache_key(name = nil, &block)
  fail "Can not provide both cache_key and a block" if name && block
  @route.cache_key = name || block
end

#expires(time) ⇒ Object

This helper is used within a controller or route to indicate how often content should persist in the cache.

After seconds seconds have passed, content previously cached will be discarded and re-rendered. Code associated with that route will not be executed; rather, its previous output will be sent to the client with a 200 OK status code.

Examples:

controller '/blog', :cache => true do
  expires 15

  get '/entries' do
    'Just broke up eating twinkies, lol'
  end
end


62
63
64
# File 'padrino-cache/lib/padrino-cache/helpers/page.rb', line 62

def expires(time)
  @route.cache_expires = time
end