Class: BrainzLab::Vault::Cache
- Inherits:
-
Object
- Object
- BrainzLab::Vault::Cache
- Defined in:
- lib/brainzlab/vault/cache.rb
Instance Method Summary collapse
- #clear! ⇒ Object
- #delete(key) ⇒ Object
- #delete_pattern(pattern) ⇒ Object
- #get(key) ⇒ Object
- #has?(key) ⇒ Boolean
-
#initialize(ttl = 300) ⇒ Cache
constructor
A new instance of Cache.
- #set(key, value) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(ttl = 300) ⇒ Cache
Returns a new instance of Cache.
6 7 8 9 10 |
# File 'lib/brainzlab/vault/cache.rb', line 6 def initialize(ttl = 300) @ttl = ttl @store = {} @mutex = Mutex.new end |
Instance Method Details
#clear! ⇒ Object
55 56 57 58 59 |
# File 'lib/brainzlab/vault/cache.rb', line 55 def clear! @mutex.synchronize do @store.clear end end |
#delete(key) ⇒ Object
42 43 44 45 46 |
# File 'lib/brainzlab/vault/cache.rb', line 42 def delete(key) @mutex.synchronize do @store.delete(key) end end |
#delete_pattern(pattern) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/brainzlab/vault/cache.rb', line 48 def delete_pattern(pattern) @mutex.synchronize do regex = Regexp.new(pattern.gsub('*', '.*')) @store.delete_if { |k, _| k.match?(regex) } end end |
#get(key) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/brainzlab/vault/cache.rb', line 12 def get(key) @mutex.synchronize do entry = @store[key] return nil unless entry return nil if expired?(entry) entry[:value] end end |
#has?(key) ⇒ Boolean
32 33 34 35 36 37 38 39 40 |
# File 'lib/brainzlab/vault/cache.rb', line 32 def has?(key) @mutex.synchronize do entry = @store[key] return false unless entry return false if expired?(entry) true end end |
#set(key, value) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/brainzlab/vault/cache.rb', line 22 def set(key, value) @mutex.synchronize do @store[key] = { value: value, expires_at: Time.now + @ttl } end value end |
#size ⇒ Object
61 62 63 64 65 66 |
# File 'lib/brainzlab/vault/cache.rb', line 61 def size @mutex.synchronize do cleanup_expired! @store.size end end |