Class: Resourceful::Resource::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/resource/cache.rb

Constant Summary collapse

EXPIRY_SECS =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expiration = EXPIRY_SECS) ⇒ Cache

Returns a new instance of Cache.



15
16
17
18
# File 'lib/resourceful/resource/cache.rb', line 15

def initialize(expiration=EXPIRY_SECS)
  @expiration = (expiration && expiration.kind_of?(::Fixnum)) ? expiration : EXPIRY_SECS
  @store = {}
end

Instance Attribute Details

#expirationObject (readonly)

Returns the value of attribute expiration.



7
8
9
# File 'lib/resourceful/resource/cache.rb', line 7

def expiration
  @expiration
end

#storeObject (readonly)

Returns the value of attribute store.



7
8
9
# File 'lib/resourceful/resource/cache.rb', line 7

def store
  @store
end

Class Method Details

.key(host, verb, resource) ⇒ Object



11
12
13
# File 'lib/resourceful/resource/cache.rb', line 11

def self.key(host, verb, resource)
  "#{host}_#{verb}_#{resource}"
end

Instance Method Details

#clear(key = nil) ⇒ Object



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

def clear(key=nil)
  if key
    @store[key] = nil
  else
    @store = {}
  end
end

#read(key) ⇒ Object



28
29
30
31
# File 'lib/resourceful/resource/cache.rb', line 28

def read(key)
  entry = @store[key]
  expired?(entry) ? nil : entry[:value]
end

#write(key, value) ⇒ Object



33
34
35
36
# File 'lib/resourceful/resource/cache.rb', line 33

def write(key, value)
  @store[key] = {:value => value, :expires => Time.now.to_i + @expiration}
  value
end