Merb::Cache::CacheMixin

Public Class Methods

included(base) click to toggle source
# File lib/merb-cache/merb_ext/controller.rb, line 2
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

_cache_after(conditions = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source

ugly, please make me purdy’er

# 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() click to toggle source
# File lib/merb-cache/merb_ext/controller.rb, line 144
def _set_skip_cache
  @_skip_cache = true
end
default_cache_store() click to toggle source

Overwrite this in your controller to change the default store for a given controller

# 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) click to toggle source
# 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) click to toggle source
# 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(%{^#<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 = {}) click to toggle source
# File lib/merb-cache/merb_ext/controller.rb, line 77
def fetch_partial(template, opts={}, conditions = {})
  template_id = template.to_s
  if template_id =~ %{^/}
    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!() click to toggle source
# File lib/merb-cache/merb_ext/controller.rb, line 152
def force_cache!
  @_force_cache = true
end
skip_cache!() click to toggle source
# File lib/merb-cache/merb_ext/controller.rb, line 148
def skip_cache!
  _set_skip_cache
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.