DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Simple Cacher Module For Rails
Illustration of a simple cacher module for Rails.
# lib/cacher.rb
module Cacher
STORE = {}
def cache(*args)
STORE[args.inspect] ||= yield
end
def flush!
STORE.clear
end
module_function :flush!
end
# app/models/person.rb
class Person < AR::Base
include Cacher
def finger
cache(:finger, email) do
`finger #{email}`
end
end
end
# app/controller/application.rb
class ApplicationController < AC::Base
after_filter { Cacher.flush! }
end




