Module: Cell::Caching::ClassMethods
- Defined in:
- lib/cell/caching.rb
Instance Method Summary (collapse)
-
- (Object) cache(state, *args, &block)
Caches the rendered view of state.
- - (Boolean) cache?(state)
- - (Object) cache_store
- - (Object) expire_cache_key(key, *args)
-
- (Object) state_cache_key(state, key_parts = {})
Computes the complete, namespaced cache key for state.
Instance Method Details
- (Object) cache(state, *args, &block)
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
The :if option lets you define a conditional proc or instance method. If it doesn't return a true value, caching for that state is skipped.
cache :show, :if => proc { |cell, | [:enable_cache] }
If you need your own granular cache keys, pass a versioner block.
cache :show do |cell, |
"user/#{cell.[: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/#{[: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.
Inheritance
Please note that cache configuration is inherited to derived cells.
55 56 57 58 59 60 61 |
# File 'lib/cell/caching.rb', line 55 def cache(state, *args, &block) = args. self.conditional_procs = conditional_procs.merge(state => .delete(:if)) self.version_procs = version_procs.merge(state => (args.first || block)) self. = .merge(state => ) end |
- (Boolean) cache?(state)
78 79 80 81 |
# File 'lib/cell/caching.rb', line 78 def cache?(state) # DISCUSS: why is it private? ActionController::Base.send(:cache_configured?) and state_cached?(state) end |
- (Object) cache_store
63 64 65 66 67 |
# File 'lib/cell/caching.rb', line 63 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 |
- (Object) expire_cache_key(key, *args)
74 75 76 |
# File 'lib/cell/caching.rb', line 74 def expire_cache_key(key, *args) cache_store.delete(key, *args) end |
- (Object) state_cache_key(state, key_parts = {})
Computes the complete, namespaced cache key for state.
70 71 72 |
# File 'lib/cell/caching.rb', line 70 def state_cache_key(state, key_parts={}) ([controller_path, state, key_parts]) end |