Sha256: 050e7901f07c02fb34fc653337c2c19a81e5909d77473c3d91317484014c319d

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require "dalli"
require "alephant/logger"

module Alephant
  module Lookup
    class LookupCache
      include Logger

      attr_reader :config

      DEFAULT_TTL  = 2

      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

      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

      def set(key, value, ttl = nil)
        value.tap { |o| @client.set(versioned(key), o, ttl) }
      end

      private

      def config_endpoint
        config[:elasticache_config_endpoint] || config["elasticache_config_endpoint"]
      end

      def ttl
        config[:lookup_elasticache_ttl] || config["lookup_elasticache_ttl"] || DEFAULT_TTL
      end

      def versioned(key)
        [key, cache_version].compact.join("_")
      end

      def cache_version
        config[:elasticache_cache_version] || config["elasticache_cache_version"]
      end
    end

    class NullClient
      def get(key); end

      def set(key, value, ttl = nil)
        value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-lookup-2.2.0 lib/alephant/lookup/lookup_cache.rb