class Makitoo::FeatureFlag::MemoryCache

Public Class Methods

new(logger, ttl = 20.seconds) click to toggle source
# File lib/makitoo/feature_flag/memory_cache.rb, line 6
def initialize(logger, ttl = 20.seconds)
  @logger = logger
  @cache = {}
  @ttl = ttl
  @lock = Mutex.new
end

Public Instance Methods

get(key) click to toggle source
# File lib/makitoo/feature_flag/memory_cache.rb, line 13
def get(key)
  if @cache.has_key?(key)
    if @cache[key][:end] > Time.now()
      @logger.debug 'Cache hit'
      @cache[key][:value]
    else
      @logger.debug 'Cache invalid'
      @lock.synchronize do
        @cache.delete(key)
      end
      nil
    end
  else
    @logger.debug 'Cache miss'
    nil
  end
end
put(key, value) click to toggle source
# File lib/makitoo/feature_flag/memory_cache.rb, line 31
def put(key, value)
  @logger.debug 'Cache put'
  @lock.synchronize do
    @cache[key] = {
      value: value,
      end: Time.now() + @ttl
    }
  end
end