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