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: