Class: Phishin::Client::Cache
- Inherits:
-
Object
- Object
- Phishin::Client::Cache
- Defined in:
- lib/phishin/client/cache.rb
Constant Summary collapse
- CACHE_NAMESPACE =
'phishin-client'
Class Method Summary collapse
- .delete(key) ⇒ Object
- .disable ⇒ Object
- .enable ⇒ Object
- .fetch(key, opts = {}) ⇒ Object
- .log_cache_action(action, key, val = nil) ⇒ Object private
- .read(key) ⇒ Object
- .setup(opts = {}) ⇒ Object private
- .should_cache? ⇒ Boolean private
- .write(key, value, ttl = nil) ⇒ Object
Class Method Details
.delete(key) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/phishin/client/cache.rb', line 50 def delete(key) return nil unless should_cache? val = @underlying_cache.delete(key) log_cache_action('delete', key) if val return val end |
.disable ⇒ Object
31 32 33 |
# File 'lib/phishin/client/cache.rb', line 31 def disable @enabled = false end |
.enable ⇒ Object
27 28 29 |
# File 'lib/phishin/client/cache.rb', line 27 def enable @enabled = true end |
.fetch(key, opts = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/phishin/client/cache.rb', line 65 def fetch(key, opts={}) ttl = opts.delete(:ttl) force = opts.delete(:force) value = read(key) unless force if force || !value value = yield write(key, value, ttl) unless force end return value end |
.log_cache_action(action, key, val = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 |
# File 'lib/phishin/client/cache.rb', line 59 def log_cache_action(action, key, val=nil) ::Phishin::Client::Client.logger.info "phish.in cache #{action} key=#{key[0..8]}" if ::Phishin::Client::Client.logger end |
.read(key) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/phishin/client/cache.rb', line 35 def read(key) return nil unless should_cache? val = @underlying_cache.get(key) log_cache_action('read', key, val) if val return val end |
.setup(opts = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 16 17 18 19 20 |
# File 'lib/phishin/client/cache.rb', line 13 def setup(opts={}) @enabled = true #require 'dalli' #@underlying_cache = MemcachedCache.new(opts) require 'redis' @underlying_cache = RedisCache.new(opts) end |
.should_cache? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 |
# File 'lib/phishin/client/cache.rb', line 23 def should_cache? return @enabled && !!@underlying_cache end |
.write(key, value, ttl = nil) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/phishin/client/cache.rb', line 43 def write(key, value, ttl=nil) return nil unless should_cache? log_cache_action('write', key, value) @underlying_cache.set(key, value, ttl) end |