Class: DHC::Caching::MultilevelCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dhc/interceptors/caching.rb

Instance Method Summary collapse

Constructor Details

#initialize(central: nil, local: nil) ⇒ MultilevelCache

Returns a new instance of MultilevelCache.



18
19
20
21
# File 'lib/dhc/interceptors/caching.rb', line 18

def initialize(central: nil, local: nil)
  @central = central
  @local = local
end

Instance Method Details

#fetch(key) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dhc/interceptors/caching.rb', line 23

def fetch(key)
  central_response = @central[:read].fetch(key) if @central && @central[:read].present?
  if central_response
    DHC::Logger.info %([DHC] served from central cache: "#{key}")
    return central_response
  end
  local_response = @local.fetch(key) if @local
  if local_response
    DHC::Logger.info %([DHC] served from local cache: "#{key}")
    return local_response
  end
end

#write(key, content, options) ⇒ Object



36
37
38
39
# File 'lib/dhc/interceptors/caching.rb', line 36

def write(key, content, options)
  @central[:write].write(key, content, options) if @central && @central[:write].present?
  @local.write(key, content, options) if @local.present?
end