Class: Exchange::Cache::Redis
Overview
A class that cooperates with the redis gem and the redis key value store to cache the data from the exchange api in redis
Instance Method Summary collapse
-
#cached(api, opts = {}) { ... } ⇒ Object
returns either cached data from the redis client or calls the block and caches it in redis.
-
#client ⇒ ::Redis
instantiates a redis client and memoizes it in a class variable.
-
#wipe_client! ⇒ Object
Wipe the client instance variable.
Instance Method Details
#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
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 |
#client ⇒ ::Redis
instantiates a redis client and memoizes it in a class variable. Use this client to access redis data. For further explanation of use visit the redis gem documentation
27 28 29 30 |
# File 'lib/exchange/cache/redis.rb', line 27 def client Exchange::GemLoader.new('redis').try_load unless defined?(::Redis) @client ||= ::Redis.new(:host => config.host, :port => config.port) end |
#wipe_client! ⇒ Object
Wipe the client instance variable
34 35 36 |
# File 'lib/exchange/cache/redis.rb', line 34 def wipe_client! @client = nil end |