Class: Exchange::Cache::Memcached
- Defined in:
- lib/exchange/cache/memcached.rb
Overview
A class that cooperates with the memcached gem to cache the data from the exchange api in memcached
Instance Method Summary collapse
-
#cached(api, opts = {}) { ... } ⇒ Object
returns either cached data from the memcached client or calls the block and caches it in memcached.
-
#client ⇒ Dalli::Client
instantiates a memcached 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 memcached client or calls the block and caches it in memcached. This method has to be the same in all the cache classes in order for the configuration binding to work
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/exchange/cache/memcached.rb', line 47 def cached api, opts={}, &block stored = client.get(key(api, opts)) result = opts[:plain] ? stored : stored.decachify if stored && !stored.to_s.empty? unless result result = super if result && !result.to_s.empty? client.set key(api, opts), result.cachify, config.expire == :daily ? 86400 : 3600 end end result end |
#client ⇒ Dalli::Client
instantiates a memcached client and memoizes it in a class variable. Use this client to access memcached data. For further explanation of use visit the memcached gem documentation
28 29 30 31 |
# File 'lib/exchange/cache/memcached.rb', line 28 def client Exchange::GemLoader.new('dalli').try_load unless defined?(::Dalli) @client ||= Dalli::Client.new("#{config.host}:#{config.port}") end |
#wipe_client! ⇒ Object
Wipe the client instance variable
35 36 37 |
# File 'lib/exchange/cache/memcached.rb', line 35 def wipe_client! @client = nil end |