Module: Merb::Cache::CacheMixin

Included in:
Merb::Controller
Defined in:
merb-cache/lib/merb-cache/merb_ext/controller.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



2
3
4
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 2

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

- (Object) _cache_after(conditions = {})



117
118
119
120
121
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 117

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

- (Object) _cache_before(conditions = {})



108
109
110
111
112
113
114
115
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 108

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

- (Object) _eager_cache_after(klass, action, conditions = {}, blk = nil)



123
124
125
126
127
128
129
130
131
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 123

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

- (Object) _lookup_store(conditions = {})



159
160
161
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 159

def _lookup_store(conditions = {})
  conditions[:store] || conditions[:stores] || default_cache_store
end

- (Object) _parameters_and_conditions(conditions)

TODO:

Code: ugly, please make me purdy'er



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
194
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 169

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

- (Object) _set_skip_cache



147
148
149
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 147

def _set_skip_cache
  @_skip_cache = true
end

- (Object) default_cache_store

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



164
165
166
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 164

def default_cache_store
  Merb::Cache.default_store_name
end

- (Object) eager_cache(action, conditions = {}, params = request.params.dup, env = request.env.dup, &blk)



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 133

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

- (Object) fetch_fragment(opts = {}, conditions = {}, &proc)



96
97
98
99
100
101
102
103
104
105
106
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 96

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

- (Object) fetch_partial(template, opts = {}, conditions = {})



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 80

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

- (Object) force_cache!



155
156
157
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 155

def force_cache!
  @_force_cache = true
end

- (Object) skip_cache!



151
152
153
# File 'merb-cache/lib/merb-cache/merb_ext/controller.rb', line 151

def skip_cache!
  _set_skip_cache
end