Module: Cell::Caching::ClassMethods

Defined in:
lib/cell/caching.rb

Instance Method Summary (collapse)

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, options| options[:enable_cache] }

If you need your own granular cache keys, pass a versioner block.

cache :show do |cell, options|
  "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(options)
  "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.

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)
  options = args.extract_options!

  self.conditional_procs = conditional_procs.merge(state => options.delete(:if))
  self.version_procs     = version_procs.merge(state => (args.first || block))
  self.cache_options     = cache_options.merge(state => options)
end

- (Object) expire_cache_key_for(key, cache_store, *args)



68
69
70
# File 'lib/cell/caching.rb', line 68

def expire_cache_key_for(key, cache_store, *args)
  cache_store.delete(key, *args)
end

- (Object) state_cache_key(state, key_parts = {})

Computes the complete, namespaced cache key for state.



64
65
66
# File 'lib/cell/caching.rb', line 64

def state_cache_key(state, key_parts={})
  expand_cache_key([controller_path, state, key_parts])
end