Sha256: 1b5a9e46d77ab9b888b2d36ba42023973170ebd86dad44cf277cec027e4c0e3f

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

= Hash Cache

Hashes make good in-memory caches.  We all do it!

I often create projects that allow some kind of caching repository to exist.
Usually, I use an instance of a Hash during development, but I might actually 
want to swap it out with memcached or another cache store, in production.

Whenever I do this, I always have to add #get and #set methods to Hash, as these 
methods are pretty conventional for cache stores.

Well ... I'm sick of it.  Introducting a mini RubyGem that does this for you.

== Installation

  sudo gem install devfu-hash-cache --source http://gems.github.com

== Usage

  >> cache = { }
  => {}

  >> cache.set 'chunky', 'bacon'
  NoMethodError: undefined method `set' for {}:Hash
          from (irb):2

  >> require 'hash/cache'
  => true

  >> cache.set 'chunky', 'bacon'
  => "bacon"

  >> cache
  => {"chunky"=>"bacon"}

Ta Da!

If you won't want to apply these methods to all Hashes, just a particular instance of a Hash:

  >> require 'hash-cache'
  => true

  >> cache = { }
  => {}

  >> cache.set 'chunky', 'bacon'
  NoMethodError: undefined method `set' for {}:Hash
          from (irb):2

  >> cache = Hash::Cache.new(cache)
  => {}

  >> cache.set 'chunky', 'bacon'
  => "bacon"

  >> cache
  => {"chunky"=>"bacon"}

  >> Hash.new.get 'foo'
  NoMethodError: undefined method `get' for {}:Hash
    from (irb):11

For more information, check out:
RDoc::  http://code.devfu.com/hash-cache
Specs:: http://github.com/devfu/hash-cache/blob/master/spec/hash-cache_spec.rb

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
devfu-hash-cache-0.1.0 README.rdoc
devfu-hash-cache-0.1.1 README.rdoc