Class: Makitoo::FeatureFlag::MemoryCache
- Inherits:
-
Object
- Object
- Makitoo::FeatureFlag::MemoryCache
- Defined in:
- lib/makitoo/feature_flag/memory_cache.rb
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize(logger, ttl = 20.seconds) ⇒ MemoryCache
constructor
A new instance of MemoryCache.
- #put(key, value) ⇒ Object
Constructor Details
#initialize(logger, ttl = 20.seconds) ⇒ MemoryCache
Returns a new instance of MemoryCache.
6 7 8 9 10 11 |
# 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 |
Instance Method Details
#get(key) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# 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 |