Module: Itrigga::Cache::ControllerMethods

Defined in:
lib/itrigga/cache/cache.rb

Instance Method Summary collapse

Instance Method Details

#with_controller_cache(opts = {}, &block) ⇒ Object



159
160
161
162
163
164
165
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
192
193
194
# File 'lib/itrigga/cache/cache.rb', line 159

def with_controller_cache( opts = {}, &block )  

  if caching_enabled?(opts)
  
    opts[:key] = opts[:cache_key] || opts[:key] || (respond_to?(:cache_key) ? cache_key.to_s : nil)  # backwards compat using :cache_key
    raise ArgumentError("No cache key found") unless opts[:key]
    
    begin
    
      if params && params.kind_of?(Hash) && params["freshen"]
        delete_from_cache opts[:key], opts       
      end
        
      @content = with_cache(opts, &block) 
      @content ||= render_to_string unless performed?                
          
    rescue Exception => ex
      cache_log "Error when rendering cache block: #{ex.message}", opts
      @content ||= block.call
      @content ||= render_to_string unless performed?
    end
    
    # flashes need to be rendered and replaced every request if we're in a html request
    render_flashes if request.format.html? && respond_to?(:render_flashes)
  
  else # cache is not enabled so just render the block
    cache_log "Cache not enabled!", opts
    @content ||= block.call
    @content ||= render_to_string unless performed?
  end
  
  render(:text=>@content, :content_type=>( @content_type || "text/html"), :status=>@status) unless performed?
  
  @content
  
end