Class: Cache
- Inherits:
-
Object
- Object
- Cache
- Defined in:
- lib/libcache/cache.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#expiry_time ⇒ Object
Returns the value of attribute expiry_time.
-
#refresh ⇒ Object
Returns the value of attribute refresh.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #create_store ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #invalidate(key) ⇒ Object
- #invalidateAll ⇒ Object
- #put(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 |
# File 'lib/libcache/cache.rb', line 7 def initialize ENV['TZ'] = 'UTC' @scheduler = Rufus::Scheduler.new end |
Instance Attribute Details
#expiry_time ⇒ Object
Returns the value of attribute expiry_time.
5 6 7 |
# File 'lib/libcache/cache.rb', line 5 def expiry_time @expiry_time end |
#refresh ⇒ Object
Returns the value of attribute refresh.
5 6 7 |
# File 'lib/libcache/cache.rb', line 5 def refresh @refresh end |
#store ⇒ Object
Returns the value of attribute store.
5 6 7 |
# File 'lib/libcache/cache.rb', line 5 def store @store end |
Instance Method Details
#create_store ⇒ Object
12 13 14 |
# File 'lib/libcache/cache.rb', line 12 def create_store @cache = Hash.new end |
#get(key) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/libcache/cache.rb', line 23 def get(key) if @cache[key] == nil return refresh.call key end return @cache[key] end |
#invalidate(key) ⇒ Object
30 31 32 |
# File 'lib/libcache/cache.rb', line 30 def invalidate(key) @cache.delete key end |
#invalidateAll ⇒ Object
34 35 36 |
# File 'lib/libcache/cache.rb', line 34 def invalidateAll @cache.clear end |
#put(key, value) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/libcache/cache.rb', line 16 def put(key, value) @cache[key] = value @scheduler.in expiry_time, :blocking => true do invalidate key end end |