public
Description: has_cache is plugin that wraps Rails cache features into a simple automatic way.
Homepage:
Clone URL: git://github.com/fnando/has_cache.git
fnando (author)
Tue Nov 25 20:02:42 -0800 2008
commit  fe6277e3f26df1a652eba51a5eef4e9a56acdf60
tree    94987caf903e1f8415ca6f78aba2b10ef5ad65b9
parent  587fb963c7552cbbcf9a2ffbd9675235df56a271
name age message
file .gitignore Loading commit data...
file MIT-LICENSE
file README.markdown
file init.rb
directory lib/
directory spec/
directory tasks/
README.markdown

has_cache

has_cache is a plugin that wraps Rails cache features into a simple automatic way. All associations has a cached_[name] version. If you need to cache additional methods, you can use the :include option.

This plugin also has an :expire option that is called everytime a object is saved, destroyed or when the expire_cache! method is called.

Instalation

1) Install the plugin with script/plugin install git://github.com/fnando/has_cache.git

Usage

1) Add the method call has_cache to your model.

class Post < ActiveRecord::Base
  has_many :comments
  has_many: categories
  has_many :tags
  belongs_to :author, :class_name => 'User'

  has_cache :include => :find_sorted_comments, 
    :expires_in => 15.minutes,
    :expire => proc{|post| post.do_something },
    :before_expire => proc{|post| post.do_something }

  def find_sorted_comments
    comments.find(:all, :order => "created_at desc")
  end
end

post = Post.cached_find(:all_posts, :all, :conditions => {:permalink => 'some-permalink'})
post.expire_cache!
post.cached_categories
post.cached_comments
post.cached_author
post.cached_find_sorted_comments

NOTE: Class methods aren't expired automatically. Be sure to use the :before_expire option to add your own keys to the expiration list.

2) On your controller you can use cached_render

class PostsController < ApplicationController
  # this will cache only the view
  def show
   @user = User.find(params[:id])
   cached_render :name => @user.cache_key
  end

  # this will cache the whole page (layout + action)
  def show
    @user = User.find(params[:id])
    cached_render :name => @user.cache_key, :layout => true
  end

  # or

  def show
    @user = User.find(params[:id])
    cached_render :expires_in => 15.minutes
  end

  # or

  def show
    @user = User.find(params[:id])
    cached_render do
      render :layout => false
    end
  end
end

3) On your view you can use cached_block

<% cached_block :some_list, :expires_in => 15.minutes do %>
    // do something
<% end %>

# or

<% cached_block "some_list", :expires_in => 15.minutes do %>
    // do something
<% end %>

# or

<% cached_block ['some', 'list'], :expires_in => 15.minutes do %>
    // do something
<% end %>

ATTENTION: To activate controller and view cache support, you have to set ActionController::Base.perform_caching to true on your environment configuration file.

# config/environments/development.rb
config.action_controller.perform_caching = true

MAINTAINER

LICENSE:

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.