Class: Eld::Cache::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/eld/cache/memory.rb

Instance Method Summary collapse

Constructor Details

#initializeMemory

Returns a new instance of Memory.



6
7
8
# File 'lib/eld/cache/memory.rb', line 6

def initialize
  @storage = {}
end

Instance Method Details

#clearObject



28
29
30
31
32
# File 'lib/eld/cache/memory.rb', line 28

def clear
  @storage = {}

  nil
end

#fetch(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/eld/cache/memory.rb', line 10

def fetch(&block)
  current_time = Time.now.utc.to_i

  if @storage[:data] && @storage[:expires_at] > current_time
    @storage[:data]
  else
    set(&block)
  end
end

#set(&block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/eld/cache/memory.rb', line 20

def set(&block)
  response = block.call

  @storage = response

  response[:data]
end