Virtual Attribute Cache

The ultimate way to cache Active Record virtual attributes.

Installation

As a RubyGem:

gem install va_cache

(Note: GitHub still hosts va_cache with my old username.)

As a Rails plugin:

script/plugin install git://github.com/dtrasbo/va_cache.git

Usage

Virtual Attribute Cache extends Active Record with a cached_virtual_attribute method for you to use in your models. Provide the name of the virtual attribute to be cached and a proc, symbol, or string indicating when to expire the cache:

cached_virtual_attribute(:full_name, :expire_if => Proc.new { |p| p.first_name_changed? || p.last_name_changed? }) do
  [first_name, last_name].join(' ')
end

All you need is a column of the same name as the virtual attribute. In the following case, if body_textile_changed? returns true, the cache will be expired:

cached_virtual_attribute(:body, :expire_if => :body_textile_changed) do
  RedCloth.new(body_textile).to_html
end

Note: Virtual Attribute Cache will suffix the question mark (?) automatically.

Copyright © 2010 David Trasbo. Virtual Attribute Cache is released under the terms of the MIT License. See LICENSE.txt for further details.