Module: Merb::Cache::ControllerClassMethods
- Included in:
- Merb::Controller
- Defined in:
- lib/merb_cache_more/cache-page.rb,
lib/merb_cache_more/cache-action.rb
Instance Method Summary collapse
-
#cache_action(action, from_now = nil, opts = {}) ⇒ Object
Register the action for action caching.
-
#cache_actions(*actions) ⇒ Object
Register actions for action caching (before and after filters).
-
#cache_page(action, from_now = nil, opts = {}) ⇒ Object
Register the action for page caching.
-
#cache_pages(*pages) ⇒ Object
Register actions for page caching (before and after filters).
Instance Method Details
#cache_action(action, from_now = nil, opts = {}) ⇒ Object
Register the action for action caching
Parameters
- action<Symbol>
-
The name of the action to register
- from_now<~minutes>
-
The number of minutes (from now) the cache should persist
- options<Hash>
-
Key formats :format => :snake|:tree|:hash|:query or nil for default Custom format :format => “:param1/:param2-and_:param3” Params :params => [params to include] or false to disable
Examples
cache_action :mostly_static
cache_action :barely_dynamic, 10
cache_action :barely_dynamic, 10, {:format => :hash, :params => [:id, :name]}
cache_action :barely_dynamic, :format => ":param1/:param2-and_:param3[key]"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/merb_cache_more/cache-action.rb', line 25 def cache_action(action, from_now = nil, opts = {}) from_now, opts = nil, from_now if Hash === from_now cache_opts = {:format => opts[:format], :params => opts[:params]} opts.delete(:format); opts.delete(:params); opts.delete(:exclude) before("cache_#{action}_before", opts.merge(:only => action, :with => [cache_opts])) after("cache_#{action}_after", opts.merge(:only => action, :with => [cache_opts])) alias_method "cache_#{action}_before", :cache_action_before alias_method "cache_#{action}_after", :cache_action_after _actions = Merb::Cache.cached_actions[controller_name] ||= {} _actions[action] = from_now end |
#cache_actions(*actions) ⇒ Object
Register actions for action caching (before and after filters)
Parameter
- actions<Symbol,Array>
-
See #cache_action
Example
cache_actions :mostly_static, :barely_dynamic
cache_actions :mostly_static, [:barely_dynamic, 10]
cache_actions :barely_dynamic, [:barely_dynamic, 10, :format => :hash]
cache_actions :barely_dynamic, [:barely_dynamic, 10, :params => [:id, :name]]
cache_actions :all, 10, :exclude => [:show], :format => :snake, :params => [:id, :name]
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/merb_cache_more/cache-action.rb', line 51 def cache_actions(*actions) config = Merb::Plugins.config[:merb_cache] if actions[0] == :all from_now = Hash === actions[1] ? config[:cache_action_ttl] : actions[1] opts = Hash === actions[1] ? actions[1] : actions[2] || {} excludes = opts[:exclude] || [] actions = self.instance_methods(false).map {|action| [action.to_sym, from_now, opts] unless excludes.include?(action.to_sym) }.compact end actions.each do |act_opts| if Array === act_opts action = act_opts[0] from_now = Hash === act_opts[1] ? config[:cache_action_ttl] : act_opts[1] opts = Hash === act_opts[1] ? act_opts[1] : act_opts[2] else action = act_opts from_now = config[:cache_action_ttl] opts = {} end cache_action(action, from_now, opts||{}) end true end |
#cache_page(action, from_now = nil, opts = {}) ⇒ Object
Register the action for page caching
Parameters
- action<Symbol>
-
The name of the action to register
- from_now<~minutes>
-
The number of minutes (from now) the cache should persist
- options<Hash>
-
Key formats :format => :snake|:tree|:hash|:query or nil for default Custom format :format => “:param1/:param2-and_:param3” Params :params => [params to include] or false to disable
Examples
cache_page :mostly_static
cache_page :barely_dynamic, 10
cache_page :barely_dynamic, 10, {:format => :hash, :params => [:id, :name]}
cache_page :barely_dynamic, :format => ":param1/:param2-and_:param3[key]"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/merb_cache_more/cache-page.rb', line 26 def cache_page(action, from_now = nil, opts = {}) from_now, opts = nil, from_now if Hash === from_now cache_opts = {:format => opts[:format], :params => opts[:params]} opts.delete(:format); opts.delete(:params); opts.delete(:exclude) before("cache_#{action}_before", opts.merge(:only => action, :with => [cache_opts])) after("cache_#{action}_after", opts.merge(:only => action, :with => [cache_opts])) alias_method "cache_#{action}_before", :cache_page_before alias_method "cache_#{action}_after", :cache_page_after _pages = Merb::Cache.cached_pages[controller_name] ||= {} _pages[action] = [from_now, 0] end |
#cache_pages(*pages) ⇒ Object
Register actions for page caching (before and after filters)
Parameter
- actions<Symbol,Array>
-
See #cache_page
Example
cache_pages :mostly_static, :barely_dynamic
cache_pages :mostly_static, [:barely_dynamic, 10]
cache_pages :barely_dynamic, [:barely_dynamic, 10, :format => :hash]
cache_pages :barely_dynamic, [:barely_dynamic, 10, :params => [:id, :name]]
cache_pages :all, 10, :exclude => [:show], :format => :snake, :params => [:id, :name]
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/merb_cache_more/cache-page.rb', line 52 def cache_pages(*pages) config = Merb::Plugins.config[:merb_cache] if pages[0] == :all from_now = Hash === pages[1] ? config[:cache_page_ttl] : pages[1] opts = Hash === pages[1] ? pages[1] : pages[2] || {} excludes = opts[:exclude] || [] pages = self.instance_methods(false).map {|action| [action.to_sym, from_now, opts] unless excludes.include?(action.to_sym) }.compact end pages.each do |page_opts| if Array === page_opts action = page_opts[0] from_now = Hash === page_opts[1] ? config[:cache_page_ttl] : page_opts[1] opts = Hash === page_opts[1] ? page_opts[1] : page_opts[2] else action = page_opts from_now = config[:cache_page_ttl] opts = {} end cache_page(action, from_now, opts||{}) end true end |