class Phishin::Client::Cache

Constants

CACHE_NAMESPACE

Public Class Methods

delete(key) click to toggle source
# 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() click to toggle source
# File lib/phishin/client/cache.rb, line 31
def disable
  @enabled = false
end
enable() click to toggle source
# File lib/phishin/client/cache.rb, line 27
def enable
  @enabled = true
end
fetch(key, opts={}) { || ... } click to toggle source

@option opts [Integer] :ttl time to live @option opts [Boolean] :force don't use cache

# 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) click to toggle source

@api private

# 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) click to toggle source
# 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={}) click to toggle source

@api private

# 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?() click to toggle source

@api private

# File lib/phishin/client/cache.rb, line 23
def should_cache?
  return @enabled && !!@underlying_cache
end
write(key, value, ttl=nil) click to toggle source
# 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