Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A Rails plug-in that enables fragment caching in ActionMailer templates

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 caching_mailer.gemspec
= CachingMailer

Enables the use of fragment caching in ActionMailer views.

Shares the cache configuration of ActionController::Base.

== Resources

* Source:
* Support:

== Installation

  script/plugin install git://

== Usage

Configure fragment caching in "config/environment.rb" (or in the
configuration file for a specific environment such as

  config.action_controller.perform_caching = true
  config.cache_store = :file_store, File.join(Rails.root, 'tmp', 'cache')

For the mailer "app/models/hello_mailer.rb":

  class HelloMailer < ActionMailer::Base
    def hello
      subject     "Hello, World"
      recipients  ""
      from        ""

And the template "app/views/hello_mailer/hello.text.plain.erb":

  Hello, World!
  This is an computationally expensive list:
  <% cache("mail/expensive-list") do %>
     <% ExpensiveOperation.results.each do |result| %>
     * Item #<%= %>: "<%= result.title %>"
     <% end %>
  <% end %>

The computationally expensive method `ExpensiveOperation.results` will only
be invoked once.  Subsequent emails will use the cached result.

For automatic expiration, try including a time value in the cache key.  This
example will generate a new cache entry every 2 hours (removal of stale
entries is left as an exercise to the reader):

  <% cache("mail/expensive-list-#{ / 2.hours.to_i}") do %>
     <% ExpensiveOperation.results.each do |result| %>
     * Item #<%= %>: "<%= result.title %>"
     <% end %>
  <% end %>

Copyright (c) 2009 Mike Demers <mike_(at)>, released under
the MIT license.
Something went wrong with that request. Please try again.