Module: Cell::Caching::ClassMethods
- Defined in:
- lib/cell/caching.rb
Instance Method Summary collapse
-
#cache(state, *args, &block) ⇒ Object
Caches the rendered view of
state. - #cache?(state) ⇒ Boolean
- #cache_options ⇒ Object
- #cache_store ⇒ Object
- #expire_cache_key(key, *args) ⇒ Object
-
#state_cache_key(state, key_parts = {}) ⇒ Object
Computes the complete, namespaced cache key for
state. - #version_procs ⇒ Object
Instance Method Details
#cache(state, *args, &block) ⇒ Object
Caches the rendered view of state.
Examples:
This will cache forever.
class CartCell < Cell::Base
cache :show
You can also pass options to the caching engine as known from Rails caching.
cache :show, :expires_in => 10.minutes
If you need your own granular cache keys, pass a versioner block.
cache :show do |cell, |
"user/#{cell.options[:id]}"
end
This will result in a cache key like cells/cart/show/user/1.
Alternatively, use an instance method.
cache :show, :versioner
def versioner()
"user/#{options[:id]}"
end
Two things to mention here.
-
The return value of the method/block is appended to the state cache key.
-
You may return a string, a hash, an array, ActiveSupport::Caching will compile it.
40 41 42 43 44 45 |
# File 'lib/cell/caching.rb', line 40 def cache(state, *args, &block) = args. version_procs[state] = args.first || block [state] = end |
#cache?(state) ⇒ Boolean
70 71 72 73 |
# File 'lib/cell/caching.rb', line 70 def cache?(state) # DISCUSS: why is it private? ActionController::Base.send(:cache_configured?) and state_cached?(state) end |
#cache_options ⇒ Object
51 52 53 |
# File 'lib/cell/caching.rb', line 51 def ||= {} end |
#cache_store ⇒ Object
55 56 57 58 59 |
# File 'lib/cell/caching.rb', line 55 def cache_store # DISCUSS: move to instance level and delegate to #config/#parent_controller. # This would allow convenient cache settings per cell (if needed). ::ActionController::Base.cache_store end |
#expire_cache_key(key, *args) ⇒ Object
66 67 68 |
# File 'lib/cell/caching.rb', line 66 def expire_cache_key(key, *args) cache_store.delete(key, *args) end |
#state_cache_key(state, key_parts = {}) ⇒ Object
Computes the complete, namespaced cache key for state.
62 63 64 |
# File 'lib/cell/caching.rb', line 62 def state_cache_key(state, key_parts={}) ([controller_path, state, key_parts]) end |
#version_procs ⇒ Object
47 48 49 |
# File 'lib/cell/caching.rb', line 47 def version_procs @version_procs ||= {} end |