Never been to DZone Snippets before?

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 (See related posts)

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

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts