Class: Wovnrb::MemoryCache

Inherits:
CacheBase show all
Defined in:
lib/wovnrb/text_caches/memory_cache.rb

Constant Summary collapse

@@default_memory_cache_config =
{
  cache_megabytes: 200,
  ttl_seconds: 300
}

Instance Method Summary collapse

Methods inherited from CacheBase

build, get_single, reset_cache, set_single

Constructor Details

#initialize(config) ⇒ MemoryCache

Returns a new instance of MemoryCache.



11
12
13
14
15
16
# File 'lib/wovnrb/text_caches/memory_cache.rb', line 11

def initialize(config)
  @config = merge_setting(@@default_memory_cache_config, config)
  cache_size = @config[:cache_megabytes].to_f
  ttl = @config[:ttl_seconds].to_i
  @cache_store = ActiveSupport::Cache::MemoryStore.new(expires_in: ttl.seconds, size: cache_size.megabytes)
end

Instance Method Details

#get(key) ⇒ Object



22
23
24
25
# File 'lib/wovnrb/text_caches/memory_cache.rb', line 22

def get(key)
  stored_value =@cache_store.fetch(key)
  decompress(stored_value) if stored_value
end

#optionsObject



27
28
29
# File 'lib/wovnrb/text_caches/memory_cache.rb', line 27

def options
  @cache_store.options.clone
end

#put(key, value) ⇒ Object



18
19
20
# File 'lib/wovnrb/text_caches/memory_cache.rb', line 18

def put(key, value)
  @cache_store.write(key, compress(value))
end