Class: ExvoAuth::Autonomous::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/exvo_auth/autonomous/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



2
3
4
# File 'lib/exvo_auth/autonomous/cache.rb', line 2

def initialize
  @data = {}
end

Instance Method Details

#fetch(key) ⇒ Object



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

def fetch(key)
  if block_given?
    read(key) || write(key, yield)
  else
    read(key)
  end
end

#read(key) ⇒ Object



6
7
8
9
# File 'lib/exvo_auth/autonomous/cache.rb', line 6

def read(key)
  o = @data[key]
  o[:value] if o && (now - o[:timestamp]) < 3600 # cache for one hour
end

#write(key, value) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/exvo_auth/autonomous/cache.rb', line 11

def write(key, value)
  @data[key] = {
    :value     => value,
    :timestamp => now
  }
  
  value
end