Module: Merb::Cache::CacheMixin
- Included in:
- Merb::Controller
- Defined in:
- lib/merb-cache/merb_ext/controller.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #_cache_after(conditions = {}) ⇒ Object
- #_cache_before(conditions = {}) ⇒ Object
- #_eager_cache_after(klass, action, conditions = {}, blk = nil) ⇒ Object
- #_lookup_store(conditions = {}) ⇒ Object
-
#_parameters_and_conditions(conditions) ⇒ Object
ugly, please make me purdy’er.
- #_set_skip_cache ⇒ Object
-
#default_cache_store ⇒ Object
Overwrite this in your controller to change the default store for a given controller.
- #eager_cache(action, conditions = {}, params = request.params.dup, env = request.env.dup, &blk) ⇒ Object
- #fetch_fragment(opts = {}, conditions = {}, &proc) ⇒ Object
- #fetch_partial(template, opts = {}, conditions = {}) ⇒ Object
- #force_cache! ⇒ Object
- #skip_cache! ⇒ Object
Class Method Details
.included(base) ⇒ Object
2 3 4 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 2 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#_cache_after(conditions = {}) ⇒ Object
114 115 116 117 118 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 114 def _cache_after(conditions = {}) if @_skip_cache.nil? && Merb::Cache[_lookup_store(conditions)].write(self, nil, *_parameters_and_conditions(conditions)) @_cache_write = true end end |
#_cache_before(conditions = {}) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 105 def _cache_before(conditions = {}) unless @_force_cache if @_skip_cache.nil? && data = Merb::Cache[_lookup_store(conditions)].read(self, _parameters_and_conditions(conditions).first) throw(:halt, data) @_cache_hit = true end end end |
#_eager_cache_after(klass, action, conditions = {}, blk = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 120 def _eager_cache_after(klass, action, conditions = {}, blk = nil) if @_skip_cache.nil? run_later do controller = klass.eager_dispatch(action, request.params.dup, request.env.dup, blk) Merb::Cache[controller._lookup_store(conditions)].write(controller, nil, *controller._parameters_and_conditions(conditions)) end end end |
#_lookup_store(conditions = {}) ⇒ Object
156 157 158 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 156 def _lookup_store(conditions = {}) conditions[:store] || conditions[:stores] || default_cache_store end |
#_parameters_and_conditions(conditions) ⇒ Object
ugly, please make me purdy’er
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 166 def _parameters_and_conditions(conditions) parameters = {} if self.class.respond_to? :action_argument_list arguments, defaults = self.class.action_argument_list[action_name] arguments.inject(parameters) do |parameters, arg| if defaults.include?(arg.first) parameters[arg.first] = self.params[arg.first] || arg.last else parameters[arg.first] = self.params[arg.first] end parameters end end case conditions[:params] when Symbol parameters[conditions[:params]] = self.params[conditions[:params]] when Array conditions[:params].each do |param| parameters[param] = self.params[param] end end return parameters, conditions.except(:params, :store, :stores) end |
#_set_skip_cache ⇒ Object
144 145 146 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 144 def _set_skip_cache @_skip_cache = true end |
#default_cache_store ⇒ Object
Overwrite this in your controller to change the default store for a given controller
161 162 163 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 161 def default_cache_store Merb::Cache.default_store_name end |
#eager_cache(action, conditions = {}, params = request.params.dup, env = request.env.dup, &blk) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 130 def eager_cache(action, conditions = {}, params = request.params.dup, env = request.env.dup, &blk) unless @_skip_cache if action.is_a?(Array) klass, action = *action else klass = self.class end run_later do controller = klass.eager_dispatch(action, params.dup, env.dup, blk) end end end |
#fetch_fragment(opts = {}, conditions = {}, &proc) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 93 def fetch_fragment(opts = {}, conditions = {}, &proc) if opts[:cache_key].blank? file, line = proc.to_s.scan(%r{^#<Proc:0x\w+@(.+):(\d+)>$}).first fragment_key = "#{file}[#{line}]" else fragment_key = opts.delete(:cache_key) end concat(Merb::Cache[_lookup_store(conditions)].fetch(fragment_key, opts, conditions) { capture(&proc) }, proc.binding) end |
#fetch_partial(template, opts = {}, conditions = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 77 def fetch_partial(template, opts={}, conditions = {}) template_id = template.to_s if template_id =~ %r{^/} template_path = File.dirname(template_id) / "_#{File.basename(template_id)}" else kontroller = (m = template_id.match(/.*(?=\/)/)) ? m[0] : controller_name template_id = "_#{File.basename(template_id)}" end unused, template_key = _template_for(template_id, opts.delete(:format) || content_type, kontroller, template_path) fetch_proc = lambda { partial(template, opts) } concat(Merb::Cache[_lookup_store(conditions)].fetch(template_key, opts, conditions, &fetch_proc), fetch_proc.binding) end |
#force_cache! ⇒ Object
152 153 154 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 152 def force_cache! @_force_cache = true end |
#skip_cache! ⇒ Object
148 149 150 |
# File 'lib/merb-cache/merb_ext/controller.rb', line 148 def skip_cache! _set_skip_cache end |