Module: Merb::Cache::ControllerInstanceMethods
- Included in:
- Merb::Controller
- Defined in:
- lib/merb_cache_more/cache-page.rb,
lib/merb_cache_more/merb-cache.rb,
lib/merb_cache_more/cache-action.rb,
lib/merb_cache_more/cache-fragment.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.
118 119 120 |
# File 'lib/merb_cache_more/cache-action.rb', line 118 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.
140 141 142 |
# File 'lib/merb_cache_more/cache-page.rb', line 140 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_more/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_more/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_more/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_more/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]])
92 93 94 95 |
# File 'lib/merb_cache_more/cache-action.rb', line 92 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')
96 97 98 99 100 |
# File 'lib/merb_cache_more/cache-page.rb', line 96 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_more/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)
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/merb_cache_more/cache-action.rb', line 105 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.
234 235 236 |
# File 'lib/merb_cache_more/merb-cache.rb', line 234 def expire_all Merb::Controller._cache.store.expire_all end |
#expire_all_pages ⇒ Object
Expires all the pages stored in config
134 135 136 |
# File 'lib/merb_cache_more/cache-page.rb', line 134 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')
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/merb_cache_more/cache-page.rb', line 119 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 |