Module: Merb::Cache::Controller::ClassMethods
- Included in:
- Merb::Controller
- Defined in:
- lib/merb-cache/merb_ext/controller/class_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#_controller_and_conditions(action, request_hash) ⇒ Array[Controller, Hash]
private
Used by cache?, cached?, cache_for and delete_cache_for to generate the controller from the given parameters.
-
#build_request(request_hash = {}) ⇒ Request
Builds a request that will be sent to the merb worker process to be cached without holding some poor user up generating the cache (through run_later).
- #build_url(*args) ⇒ Object
-
#cache(*actions) ⇒ Object
cache is an alias to cache_action, it will take multiple :actions and pass the conditions hash for each action.
- #cache!(conditions = {}) ⇒ Object
-
#cache_action(action, conditions = {}) ⇒ Object
private
cache action will cache the action Parameters are the same as cache but only one action is allowed.
-
#cache_for(action, params = {}) ⇒ Object
Returns the cache for the action with the given request.
-
#cached?(action, params = {}) ⇒ TrueClass, FalseClass
Checks if the action called with a certain request has been cached or not.
-
#caches?(action, params = {}) ⇒ TrueClass, FalseClass
Checks if the action called with a certain request would be cached or not.
-
#delete_cache_for(action, params = {}) ⇒ Object
Deletes the cache for the action with the given request.
-
#eager_cache(trigger_actions, target = nil, opts = {}, &blk) ⇒ Object
Caches specified with eager_cache will be run after #trigger_action has been run without holding some poor user up generating the cache (through run_later).
-
#eager_dispatch(action, params = {}, env = {}, blk = nil) ⇒ Object
private
Dispatches eager caches to a worker process.
Class Method Details
.extended(base) ⇒ Object
3 4 5 6 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 3 def self.extended(base) base.send :class_inheritable_accessor, :_cache base._cache = {} end |
Instance Method Details
#_controller_and_conditions(action, request_hash) ⇒ Array[Controller, Hash]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
in the request hash, :params, :uri and :method are supported. Additionally :store and :stores specify which store to use.
Used by cache?, cached?, cache_for and delete_cache_for to generate the controller from the given parameters.
234 235 236 237 238 239 240 241 242 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 234 def _controller_and_conditions(action, request_hash) conditions = self._cache[action] conditions.merge!(request_hash.only(:store, :stores)) request_hash.extract!(:store, :stores) controller = new(build_request(request_hash)) controller.action_name = action [controller, conditions] end |
#build_request(request_hash = {}) ⇒ Request
Acceptable options for the request hash
-
:uri The uri of the request
-
:method The http method (defaults to :get)
-
:params The params
-
any env variable you may need
Builds a request that will be sent to the merb worker process to be cached without holding some poor user up generating the cache (through run_later)
212 213 214 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 212 def build_request(request_hash = {}) Merb::Cache::CacheRequest.new(request_hash.delete(:uri), request_hash.delete(:params), request_hash) end |
#build_url(*args) ⇒ Object
217 218 219 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 217 def build_url(*args) Merb::Router.url(*args) end |
#cache(*actions) ⇒ Object
Specific options for cache:
- :store (or :stores) use the specified store
- :params list of params to pass to the store (see _parameters_and_conditions)
cache is an alias to cache_action, it will take multiple :actions and pass the conditions hash for each action
30 31 32 33 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 30 def cache(*actions) = (actions) || {} actions.each {|a| cache_action(a, )} end |
#cache!(conditions = {}) ⇒ Object
9 10 11 12 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 9 def cache!(conditions = {}) before(:_cache_before, conditions.only(:if, :unless).merge(:with => conditions)) after(:_cache_after, conditions.only(:if, :unless).merge(:with => conditions)) end |
#cache_action(action, conditions = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
cache action will cache the action Parameters are the same as cache but only one action is allowed
39 40 41 42 43 44 45 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 39 def cache_action(action, conditions = {}) self._cache[action] = conditions before("_cache_#{action}_before", conditions.only(:if, :unless).merge(:with => [conditions], :only => action)) after("_cache_#{action}_after", conditions.only(:if, :unless).merge(:with => [conditions], :only => action)) alias_method "_cache_#{action}_before", :_cache_before alias_method "_cache_#{action}_after", :_cache_after end |
#cache_for(action, params = {}) ⇒ Object
if not specified, the default http method is :get and the default uri is ‘/’
Returns the cache for the action with the given request
103 104 105 106 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 103 def cache_for(action, params = {}) controller, conditions = _controller_and_conditions(action, params) Merb::Cache[controller._lookup_store(conditions)].read(controller, *controller._parameters_and_conditions(conditions).first) end |
#cached?(action, params = {}) ⇒ TrueClass, FalseClass
if not specified, the default http method is :get and the default uri is ‘/’
Checks if the action called with a certain request has been cached or not.
83 84 85 86 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 83 def cached?(action, params = {}) controller, conditions = _controller_and_conditions(action, params) Merb::Cache[controller._lookup_store(conditions)].exists?(controller, *controller._parameters_and_conditions(conditions)) end |
#caches?(action, params = {}) ⇒ TrueClass, FalseClass
if not specified, the default http method is :get and the default uri is ‘/’
Checks if the action called with a certain request would be cached or not. Usefull for troubleshooting problems
63 64 65 66 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 63 def caches?(action, params = {}) controller, conditions = _controller_and_conditions(action, params) Merb::Cache[controller._lookup_store(conditions)].writable?(controller, *controller._parameters_and_conditions(conditions)) end |
#delete_cache_for(action, params = {}) ⇒ Object
if not specified, the default http method is :get and the default uri is ‘/’
Deletes the cache for the action with the given request
121 122 123 124 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 121 def delete_cache_for(action, params = {}) controller, conditions = _controller_and_conditions(action, params) Merb::Cache[controller._lookup_store(conditions)].delete(controller, *controller._parameters_and_conditions(conditions).first) end |
#eager_cache(trigger_actions, target = nil, opts = {}, &blk) ⇒ Object
Valid request options are:
- :uri the uri of the resource you want to eager cache (needed by the page store but can be provided instead by a block)
- :method http method used (defaults to :get)
Caches specified with eager_cache will be run after #trigger_action has been run without holding some poor user up generating the cache (through run_later)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 144 def eager_cache(trigger_actions, target = nil, opts = {}, &blk) trigger_actions = [*trigger_actions] target, conditions = nil, target if target.is_a? Hash trigger_actions.each do |trigger_action| if target.is_a? Array target_controller, target_action = *target else target_controller, target_action = self, (target || trigger_action) end after("_eager_cache_#{trigger_action}_to_#{target_controller.name.snake_case}__#{target_action}_after", opts.only(:if, :unless).merge(:with => [target_controller, target_action, opts, blk], :only => trigger_action)) alias_method "_eager_cache_#{trigger_action}_to_#{target_controller.name.snake_case}__#{target_action}_after", :_eager_cache_after end end |
#eager_dispatch(action, params = {}, env = {}, blk = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dispatches eager caches to a worker process
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/merb-cache/merb_ext/controller/class_methods.rb', line 169 def eager_dispatch(action, params = {}, env = {}, blk = nil) kontroller = if blk.nil? new(build_request(env)) else result = case blk.arity when 0 then blk[] when 1 then blk[params] else blk[*[params, env]] end case result when NilClass then new(build_request(env)) when Hash, Mash then new(build_request(result)) when Merb::Request then new(result) when Merb::Controller then result else raise ArgumentError, "Block to eager_cache must return nil, the env Hash, a Request object, or a Controller object" end end kontroller.force_cache! kontroller._dispatch(action) kontroller end |