Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Elephant Cache

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 rspec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 elephant-cache.gemspec
README.rdoc

Overview

Elephant provides a caching module that may be included into a class for easy internal caching. The code for this is simple, but is common enough of a design pattern that having a reusable module is handy.

Usage is quite simple:

  1. include Elephant::Cache.

  2. In your object's initialize method, call initialize_cache.

  3. Create accessors for cachable quantities, using the cache method.

Example:

class Point

  def initialize(x=0, y=0)
    initialize_cache
    self.x = x
    self.y = y
  end

  attr_reader :x, :y

  def magnitude
    return cache_value(__method__) do
      Math.sqrt(self.x**2 + self.y**2)
    end
  end

end

Additionally, if you were to make the point mutable, you'd want to dirty the cache like so:

def x=(x)
  dirty_cache(:magnitude)
  @x = x
end

def y=(y)
  dirty_cache(:magnitude)
  @y = y
end

Or, if you want to dirty the entire cache, call dirty_cache() with no arguments.

See the Elephant::Cache module for more information.

Contact

If you have any questions, comments, concerns, patches, or bugs, you can contact me via the github repository at:

github.com/paploo/elephant

or directly via e-mail at:

jeff@paploo.net

Version History

1.0.1 - 2011-Sep-01

Added timing.

  • (FEATURE) Added the ability to turn on cache calculation timing output.

  • (FIX) Some documentation referred to cache_value as cache.

1.0.0 - 2011-Aug-28

Initial release.

Something went wrong with that request. Please try again.