Class: Wovnrb::CacheBase

Inherits:
Object
  • Object
show all
Defined in:
lib/wovnrb/text_caches/cache_base.rb

Direct Known Subclasses

MemoryCache

Constant Summary collapse

@@strategy_map =
{
    memory: :memory_cache
}
@@default_base_config =
{
  strategy: :memory
}
@@singleton_cache =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(config) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wovnrb/text_caches/cache_base.rb', line 28

def self.build(config)
  @config = @@default_base_config.merge config

  strategy = @@strategy_map[@config[:strategy]]
  raise "Invalid strategy: #{strategy}" unless strategy

  strategy_sym = strategy.to_sym
  begin
    require "wovnrb/text_caches/#{strategy_sym}"
  rescue LoadError => e
    raise "Could not find #{strategy_sym} (#{e})"
  end

  strategy_class = Wovnrb.const_get(ActiveSupport::Inflector.camelize(strategy_sym))
  strategy_class.new(config)
end

.get_singleObject



14
15
16
17
18
# File 'lib/wovnrb/text_caches/cache_base.rb', line 14

def self.get_single
  raise 'cache is not initialized' unless @@singleton_cache

  @@singleton_cache
end

.reset_cacheObject



24
25
26
# File 'lib/wovnrb/text_caches/cache_base.rb', line 24

def self.reset_cache
  @@singleton_cache = nil
end

.set_single(config) ⇒ Object



20
21
22
# File 'lib/wovnrb/text_caches/cache_base.rb', line 20

def self.set_single(config)
  @@singleton_cache = build(config)
end

Instance Method Details

#get(key) ⇒ Object

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/wovnrb/text_caches/cache_base.rb', line 49

def get(key)
  raise NotImplementedError.new('put is not defined')
end

#put(key, value) ⇒ Object

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/wovnrb/text_caches/cache_base.rb', line 45

def put(key, value)
  raise NotImplementedError.new('put is not defined')
end