Skip to content

satyap/volatile_hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Implements a Time-to-live based cache and a Least-recently-used cache.

= Usage

cache = VolatileHash.new(:strategy => 'ttl', :ttl => 3600)
#  TTL-based, default TTL is 3600 seconds. The value will be cached no longer
#  than 3600 seconds after it is inserted into the cache.

cache = VolatileHash.new(:strategy => 'ttl', :ttl => 3600, :refresh => true)
#  TTL-based, default TTL is 3600 seconds. The value will be cached no longer
#  than 3600 seconds after it is inserted or accessed.

cache = VolatileHash.new(:strategy => 'lru', :max => 10)
#  LRU-based, default max is 10. That's the maximum keys to remember.

cache[:key] = "some expensive-to-calculate value, like a database query result"
cache["any key"] = "or a remote api call"

#  memo-ization:
def get_value_for(key)
    @cache ||= VolatileHash.new(:strategy => 'ttl', :ttl => 3600)
    @cache[key] ||= get_from_api(key)
end

= Test/Development

gem install bundler
bundle install
rake

= Credits

Based on a gist by github.com user joshaven at https://gist.github.com/184837

Some input from github user supertaz, considerable help on the TTL-based version.
Idea for the LRU cache developed at TrueCar Inc.

About

Implements a key-based cache with LRU or TTL policy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages