Module: Merb::Cache::ControllerInstanceMethods
- Included in:
- Merb::Controller
- Defined in:
- lib/merb-cache/cache-fragment.rb,
lib/merb-cache/cache-action.rb,
lib/merb-cache/merb-cache.rb,
lib/merb-cache/cache-page.rb
Constant Summary collapse
- DEFAULT_PAGE_EXTENSION =
Mixed in Merb::Controller. Provides methods related to page caching
'html'
Instance Method Summary collapse
-
#abort_cache_action ⇒ Object
You can call this method if you need to prevent caching the action after it has been rendered.
-
#abort_cache_page ⇒ Object
You can call this method if you need to prevent caching the page after it has been rendered.
-
#cache(options, from_now = nil, &block) ⇒ Object
Example In your view: <%- cache(“my_key”) do -%> <%= partial :test, :collection => @test %> <%- end -%>.
-
#cache_get(options) ⇒ Object
Fetch data from cache.
-
#cache_set(options, object, from_now = nil) ⇒ Object
Store data to cache.
-
#cached?(options) ⇒ Boolean
Checks whether a cache entry exists.
-
#cached_action?(options) ⇒ Boolean
Checks whether a cache entry exists.
-
#cached_page?(options) ⇒ Boolean
Checks whether a cache entry exists.
-
#expire(options) ⇒ Object
Expires the entry identified by the key computed after the parameters.
-
#expire_action(options) ⇒ Object
Expires the action identified by the key computed after the parameters.
-
#expire_all ⇒ Object
Mixed in Merb::Controller and provides expire_all for action and fragment caching.
-
#expire_all_pages ⇒ Object
Expires all the pages stored in config.
-
#expire_page(options) ⇒ Object
Expires the page identified by the key computed after the parameters.
Instance Method Details
#abort_cache_action ⇒ Object
You can call this method if you need to prevent caching the action after it has been rendered.
85 86 87 |
# File 'lib/merb-cache/cache-action.rb', line 85 def abort_cache_action @capture_action = false end |
#abort_cache_page ⇒ Object
You can call this method if you need to prevent caching the page after it has been rendered.
104 105 106 |
# File 'lib/merb-cache/cache-page.rb', line 104 def abort_cache_page @capture_page = false end |
#cache(options, from_now = nil, &block) ⇒ Object
Example
In your view:
<%- cache("my_key") do -%>
<%= partial :test, :collection => @test %>
<%- end -%>
24 25 26 27 |
# File 'lib/merb-cache/cache-fragment.rb', line 24 def cache(, from_now = nil, &block) key = Merb::Controller._cache.key_for(, controller_name) Merb::Controller._cache.store.cache(self, key, from_now, &block) end |
#cache_get(options) ⇒ Object
Fetch data from cache
Parameter
- options<String,Hash>
-
The options that will be passed to #key_for
Returns
- data<Object,NilClass>
-
nil is returned if the cache entry is not found
Example
if cache_data = cache_get("my_key")
@var1, @var2 = *cache_data
else
@var1 = MyModel.big_query1
@var2 = MyModel.big_query2
cache_set("my_key", nil, [@var1, @var2])
end
46 47 48 49 |
# File 'lib/merb-cache/cache-fragment.rb', line 46 def cache_get() key = Merb::Controller._cache.key_for(, controller_name) Merb::Controller._cache.store.cache_get(key) end |
#cache_set(options, object, from_now = nil) ⇒ Object
Store data to cache
Parameter
- options<String,Hash>
-
The options that will be passed to #key_for
- object<Object>
-
The object(s) to put in cache
- from_now<~minutes>
-
The number of minutes (from now) the cache should persist
Returns
- data<Object,NilClass>
-
nil is returned if the cache entry is not found
Example
if cache_data = cache_get("my_key")
@var1, @var2 = *cache_data
else
@var1 = MyModel.big_query1
@var2 = MyModel.big_query2
cache_set("my_key", nil, [@var1, @var2])
end
71 72 73 74 |
# File 'lib/merb-cache/cache-fragment.rb', line 71 def cache_set(, object, from_now = nil) key = Merb::Controller._cache.key_for(, controller_name) Merb::Controller._cache.store.cache_set(key, object, from_now) end |
#cached?(options) ⇒ Boolean
Checks whether a cache entry exists
Parameter
- options<String,Hash>
-
The options that will be passed to #key_for
Returns
true if the cache entry exists, false otherwise
Example
cached_action?("my_key")
14 15 16 17 |
# File 'lib/merb-cache/cache-fragment.rb', line 14 def cached?() key = Merb::Controller._cache.key_for(, controller_name) Merb::Controller._cache.store.cached?(key) end |
#cached_action?(options) ⇒ Boolean
Checks whether a cache entry exists
Parameter
- options<String,Hash>
-
The options that will be passed to #key_for
Returns
true if the cache entry exists, false otherwise
Example
cached_action?(:action => 'show', :params => [params[:page]])
59 60 61 62 |
# File 'lib/merb-cache/cache-action.rb', line 59 def cached_action?() key = Merb::Controller._cache.key_for(, controller_name, true) Merb::Controller._cache.store.cached?(key) end |
#cached_page?(options) ⇒ Boolean
Checks whether a cache entry exists
Parameter
- options<String,Hash>
-
The options that will be passed to #key_for
Returns
true if the cache entry exists, false otherwise
Example
cached_page?(:action => 'show', :params => [params[:page]])
cached_page?(:action => 'show', :extension => 'js')
60 61 62 63 64 |
# File 'lib/merb-cache/cache-page.rb', line 60 def cached_page?() key = Merb::Controller._cache.key_for(, controller_name, true) extension = [:extension] || DEFAULT_PAGE_EXTENSION File.file?(Merb::Controller._cache.config[:cache_html_directory] / "#{key}.#{extension}") end |
#expire(options) ⇒ Object
Expires the entry identified by the key computed after the parameters
Parameter
- options<String,Hash>
-
The options that will be passed to #key_for
Examples
expire("my_key")
expire(:key => "my_", :match => true)
expire(:key => "my_key", :params => [session[:me], params[:ref]])
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/merb-cache/cache-fragment.rb', line 85 def expire() Merb::Controller._cache.expire_key_for(, controller_name) do |key, match| if match Merb::Controller._cache.store.expire_match(key) else Merb::Controller._cache.store.expire(key) end end true end |
#expire_action(options) ⇒ Object
Expires the action identified by the key computed after the parameters
Parameter
- options<String,Hash>
-
The options that will be passed to #expire_key_for
Examples
expire_action(:action => 'show', :controller => 'news')
expire_action(:action => 'show', :match => true)
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/merb-cache/cache-action.rb', line 72 def expire_action() Merb::Controller._cache.expire_key_for(, controller_name, true) do |key, match| if match Merb::Controller._cache.store.expire_match(key) else Merb::Controller._cache.store.expire(key) end end true end |
#expire_all ⇒ Object
Mixed in Merb::Controller and provides expire_all for action and fragment caching.
143 144 145 |
# File 'lib/merb-cache/merb-cache.rb', line 143 def expire_all Merb::Controller._cache.store.expire_all end |
#expire_all_pages ⇒ Object
Expires all the pages stored in config
98 99 100 |
# File 'lib/merb-cache/cache-page.rb', line 98 def expire_all_pages FileUtils.rm_rf(Dir.glob(Merb::Controller._cache.config[:cache_html_directory] / "*")) end |
#expire_page(options) ⇒ Object
Expires the page identified by the key computed after the parameters
Parameter
- options<String,Hash>
-
The options that will be passed to #expire_key_for
Examples (See Merb::Cache#expire_key_for for more options)
# will expire path/to/page/cache/news/show/1.html
expire_page(:key => url(:news,News.find(1)))
# will expire path/to/page/cache/news/show.html
expire_page(:action => 'show', :controller => 'news')
# will expire path/to/page/cache/news/show*
expire_page(:action => 'show', :match => true)
# will expire path/to/page/cache/news/show.js
expire_page(:action => 'show', :extension => 'js')
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/merb-cache/cache-page.rb', line 83 def expire_page() config_dir = Merb::Controller._cache.config[:cache_html_directory] Merb::Controller._cache.expire_key_for(, controller_name, true) do |key, match| if match files = Dir.glob(config_dir / "#{key}*") else extension = [:extension] || DEFAULT_PAGE_EXTENSION files = config_dir / "#{key}.#{extension}" end FileUtils.rm_rf(files) end true end |