class Alephant::Lookup::LookupCache

Constants

DEFAULT_TTL

Attributes

config[R]

Public Class Methods

new(config={}) click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 13
def initialize(config={})
  @config = config

  unless config_endpoint.nil?
    @client ||= ::Dalli::Client.new(config_endpoint, { :expires_in => ttl })
  else
    logger.debug "Alephant::LookupCache::#initialize: No config endpoint, NullClient used"
    logger.metric "NoConfigEndpoint"
    @client = NullClient.new
  end
end

Public Instance Methods

get(key, &block) click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 25
def get(key, &block)
  begin
    versioned_key = versioned key
    result = @client.get versioned_key
    logger.info "Alephant::LookupCache#get key: #{versioned_key} - #{result ? 'hit' : 'miss'}"
    logger.metric "GetKeyMiss" unless result
    result ? result : set(key, block.call)
  rescue StandardError => e
    block.call if block_given?
  end
end
set(key, value, ttl = nil) click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 37
def set(key, value, ttl = nil)
  value.tap { |o| @client.set(versioned(key), o, ttl) }
end

Private Instance Methods

cache_version() click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 55
def cache_version
  config[:elasticache_cache_version] || config["elasticache_cache_version"]
end
config_endpoint() click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 43
def config_endpoint
  config[:elasticache_config_endpoint] || config["elasticache_config_endpoint"]
end
ttl() click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 47
def ttl
  config[:lookup_elasticache_ttl] || config["lookup_elasticache_ttl"] || DEFAULT_TTL
end
versioned(key) click to toggle source
# File lib/alephant/lookup/lookup_cache.rb, line 51
def versioned(key)
  [key, cache_version].compact.join("_")
end