class AwsDevUtils::Cache

Attributes

backend[W]

Injectable backend.

Public Instance Methods

fetch(key, exp=60, &block) click to toggle source

Returns a value from the cache for the given key. If the key can't be found, the block will be run and its result returned and be set in the cache. @param key [Object] @param exp [Integer] @param block [block] - called with no arguments, and returns the new value for the cache (if no cached data is found). @return [Object] the value from the cache or the result of the block

# File lib/aws-dev-utils/cache.rb, line 22
def fetch key, exp=60, &block
  get(key) or block.().tap {|x| set(key, x, exp)}
end
get(key) click to toggle source

Get the value of key. If not found, returns nil

# File lib/aws-dev-utils/cache.rb, line 27
def get key
  deserialize backend.get key.to_s rescue nil
end
set(key, value, expiration) click to toggle source

Set key to hold the value and set key to timeout after the a given expiration time(in seconds). @param key [Object] @param value [Object] @param expiration [Time,Integer] - the key-value timeout

# File lib/aws-dev-utils/cache.rb, line 35
def set key, value, expiration
  backend.set key.to_s, serialize(value), expiration rescue nil
end

Private Instance Methods

backend() click to toggle source
# File lib/aws-dev-utils/cache.rb, line 41
def backend
  @backend ||= AwsDevUtils::Backend::Memory.new
end
deserialize(obj) click to toggle source
# File lib/aws-dev-utils/cache.rb, line 49
def deserialize obj
  Marshal.load obj
end
serialize(obj) click to toggle source
# File lib/aws-dev-utils/cache.rb, line 45
def serialize obj
  Marshal.dump obj
end