A simple key value store where pairs can expire after a set interval
Add this to your application's shard.yml
:
dependencies:
cache_hash:
github: samueleaton/cache-hash
require "cache_hash"
cache_hash = CacheHash(String).new(2.seconds)
cache_hash.set "city1", "Seattle"
sleep 1 # one second elapsed
cache_hash.set "city2", "Hong Kong"
sleep 1 # two seconds elapsed
cache_hash.get "city1" #=> nil
cache_hash.get "city2" #=> "Hong Kong"
Defines the type(s) for the values.
CacheHash(String).new(1.minute)
Creates a new instance of CacheHash
and sets the cache interval.
Adds a key/value pair to the hash, where V
is the type(s) defined at CacheHash(V)
, and saves the time of the action.
Returns the value for the the associated key. If the pair is stale (expired) or does not exists, it returns nil
. If it exists but is expired, it is deleted before returning nil
.
Removes all key/value pairs from the hash.
Removes all stale key/value pairs from the hash.
Sets an interval where key/value pairs will automatically be purged.
Example:
cache_hash = CacheHash(String).new(1.minute)
cache_hash.set_purge_interval(10.minutes) # stale_only defaults to true
cache_hash = CacheHash(String).new(1.minute)
cache_hash.set_purge_interval(10.minutes, stale_only: false) # deletes all values, not just stale ones
Runs purge_stale
and returns an array of all the the non-stale keys.
Returns true
if the key/value pair exists and is not stale. If the pair is stale (expired) or does not exists, it returns false
. If it exists but is expired, it is deleted before returning false
.
Returns the time the key/value pair was cached. If the pair is stale (expired) or does not exists, it returns nil
. If it exists but is expired, it is deleted before returning nil
.
Refreshes the time for the key/value pair and returns the hash value if successful, otherwise returns nil
.
- Fork it ( https://github.com/samueleaton/cache_hash/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- Sam Eaton - creator, maintainer