Class: Logster::Cache
- Inherits:
-
Object
- Object
- Logster::Cache
- Defined in:
- lib/logster/cache.rb
Instance Method Summary collapse
- #clear(key) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(age = 2) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(age = 2) ⇒ Cache
Returns a new instance of Cache.
5 6 7 8 |
# File 'lib/logster/cache.rb', line 5 def initialize(age = 2) @age = age @hash = {} end |
Instance Method Details
#clear(key) ⇒ Object
17 18 19 |
# File 'lib/logster/cache.rb', line 17 def clear(key) @hash.delete(key) end |
#fetch(key) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/logster/cache.rb', line 10 def fetch(key) if !@hash.key?(key) || @hash[key][:created_at] + @age < Process.clock_gettime(Process::CLOCK_MONOTONIC) @hash[key] = { data: yield, created_at: Process.clock_gettime(Process::CLOCK_MONOTONIC) } end @hash[key][:data] end |