Module: Cell::Caching::ClassMethods

Defined in:
lib/cell/caching.rb

Instance Method Summary collapse

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, 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.



40
41
42
43
44
45
# File 'lib/cell/caching.rb', line 40

def cache(state, *args, &block)
  options = args.extract_options!
  
  version_procs[state]  = args.first || block
  cache_options[state]  = options
end

#cache?(state) ⇒ Boolean

Returns:

  • (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_optionsObject



51
52
53
# File 'lib/cell/caching.rb', line 51

def cache_options
  @cache_options ||= {}
end

#cache_storeObject



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={})
  expand_cache_key([controller_path, state, key_parts])
end

#version_procsObject



47
48
49
# File 'lib/cell/caching.rb', line 47

def version_procs
  @version_procs ||= {}
end