Class: BrainzLab::Cortex::Cache
- Inherits:
-
Object
- Object
- BrainzLab::Cortex::Cache
- Defined in:
- lib/brainzlab/cortex/cache.rb
Instance Method Summary collapse
- #clear! ⇒ Object
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
- #has?(key) ⇒ Boolean
-
#initialize(ttl = 60) ⇒ Cache
constructor
A new instance of Cache.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(ttl = 60) ⇒ Cache
Returns a new instance of Cache.
6 7 8 9 10 11 |
# File 'lib/brainzlab/cortex/cache.rb', line 6 def initialize(ttl = 60) @ttl = ttl @store = {} @timestamps = {} @mutex = Mutex.new end |
Instance Method Details
#clear! ⇒ Object
42 43 44 45 46 47 |
# File 'lib/brainzlab/cortex/cache.rb', line 42 def clear! @mutex.synchronize do @store.clear @timestamps.clear end end |
#delete(key) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/brainzlab/cortex/cache.rb', line 35 def delete(key) @mutex.synchronize do @store.delete(key) @timestamps.delete(key) end end |
#get(key) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/brainzlab/cortex/cache.rb', line 13 def get(key) @mutex.synchronize do return nil unless @store.key?(key) return nil if expired?(key) @store[key] end end |
#has?(key) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/brainzlab/cortex/cache.rb', line 29 def has?(key) @mutex.synchronize do @store.key?(key) && !expired?(key) end end |
#set(key, value) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/brainzlab/cortex/cache.rb', line 22 def set(key, value) @mutex.synchronize do @store[key] = value @timestamps[key] = Time.now end end |