Method: Exchange::Cache::Redis#cached

Defined in:
lib/exchange/cache/redis.rb

#cached(api, opts = {}) { ... } ⇒ Object

returns either cached data from the redis client or calls the block and caches it in redis. This method has to be the same in all the cache classes in order for the configuration binding to work

Parameters:

  • api (Exchange::ExternalAPI::Subclass)

    The API class the data has to be stored for

  • opts (Hash) (defaults to: {})

    the options to cache with

Options Hash (opts):

  • :at (Time)

    the historic time of the exchange rates to be cached

Yields:

  • This method takes a mandatory block with an arity of 0 and calls it if no cached result is available

Raises:

Since:

  • 0.1



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/exchange/cache/redis.rb', line 46

def cached api, opts={}, &block         
  if result = client.get(key(api, opts))
    result = opts[:plain] ? result : result.decachify
  else
    result = super
    if result && !result.to_s.empty?
      client.set key(api, opts), result.cachify
      client.expire key(api, opts), config.expire == :daily ? 86400 : 3600
    end
  end
  
  result
end