Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Wallet, a simple Ruby DSL for managing action cache configuration inside a Rails 3 app.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 features
Octocat-spinner-32 lib
Octocat-spinner-32 test_wallet_app
Octocat-spinner-32 test_wallet_engine
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 PUBLIC_DOMAIN
Octocat-spinner-32 VERSION
Octocat-spinner-32 readme.markdown
readme.markdown

Wallet

Wallet, a simple ruby DSL for centralizing action cache configuration inside your Rails app.

Installation

Wallet is a gem, available from http://rubygems.org.

Add the following to your Rails 3 app's Gemfile:

gem 'wallet', '~> 1.0.0.beta.2'

Usage

Let's imagine your app has a controller HomeController, and you want to action cache the index action inside of it. First, create a config/wallet.rb inside your rails root, then open your Wallet and add some cash:

Wallet.open do
  cash :home do
    index
  end
end

Next, open your ApplicationController, and add the following into it:

class ApplicationController < ActionController::Base
  include Wallet::Cash
  cash!
end

The cash! method will setup action caching for your controllers (if they have any cache configuration in Wallet).

With this code in place, Rails will action cache the index action inside your home controller for the default TTL (time-to-live) duration: 10 minutes. You can change the default ttl by passing a duration to the default_ttl method:

Wallet.default_ttl = 2.minutes

Now, suppose we have another controller, ArticlesController, and we want to cache the show and index actions on it for a duration of 20 minutes. Simple!

Wallet.open do
  cash :articles, :for => 20.minutes do
    show
    index
  end
end

This will cache only the show and index actions on the ArticlesController.

Now imagine we've added a third action, comments, to our ArticlesController, and we want to cache it for only 1 minute:

Wallet.open do
  cash :articles, :for => 20.minutes do
    show
    index
    comments 1.minute
  end
end

Before filters and action caching

Sometimes, even though an action is cached, you want a before filter to run before Rails try's to serve out the cached response. Authentication is one common use case. You can accomplish this with Wallet by simply calling cash! after the before filters:

class ApplicationController < ActionController::Base
  include Wallet::Cash

  before_filter :authentication
  cash!

  private
  def authentication
    #...
  end
end

Now, the authentication method will run before your before filters.

Engines

Wallet plays nice with engines. If you'd like to use Wallet inside your engine, create a config/wallet.rb file inside your engine, then include Wallet::Railtie inside your Engine railtie:

module Comments class Engine < Rails::Engine include Wallet::Railtie end end

Now, Rails will load the config/wallet.rb inside your engine before your controllers load.

Something went wrong with that request. Please try again.