Project

cache_hash

0.0
No commit activity in last 3 years
No release in over 3 years
Simple hash with expiring values. * Simple - Returns value or nil. * Default - Returns value or default * Block - Returns value, or refreshes the value by yielding to long running job.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.6
~> 10.0
 Project Readme

CacheHash

Simple hash with expiring values.

  • Simple - Returns value or nil.
  • Default - Returns value or default
  • Block - Returns value, or refreshes the value by yielding to long running job.

Installation

Add this line to your application's Gemfile:

gem 'cache_hash'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cache_hash

Usage

Simple

Returns value or nil.

c = CacheHash.new(ttl: 3)
c[:a] = "its here"
puts c[:a] # => "its here" or nil if called after 3 seconds

Default Value

Returns value or default.

x = 0
c[:foo] = "HAZ VALUE"
c.fetch(:foo, "NO VALUE") # => "HAZ VALUE"
# wait 3 seconds
c.fetch(:foo, "NO VALUE") # => "NO VALUE"

Block

Returns value, or refreshes the value by yielding to long running job.

c = CacheHash.new(ttl: 5)
users = ['smith', 'jones']

# first run loads cache

users.each do |user|
   row = c.fetch(user) {
    # long running job
    get_user(user)
  }
end

# second run hits cache

users.each do |user|
   row = c.fetch(user) {
    # long running job
    get_user(user)
  }
end


# third run refreshes cache

sleep 6
users.each do |user|
   row = c.fetch(user) {
    # long running job
    get_user(user)
  }
end


c = CacheHash.new(ttl: 2, gc_interval: 1)

Contributing

  1. Fork it ( https://github.com/[my-github-username]/cache_hash/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request