Class: Jahuty::Cache::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/jahuty/cache/facade.rb

Overview

Abstracts away the differences in cache implementation methods and argument lists.

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ Facade

Returns a new instance of Facade.



8
9
10
# File 'lib/jahuty/cache/facade.rb', line 8

def initialize(cache)
  @cache = cache
end

Instance Method Details

#delete(key) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/jahuty/cache/facade.rb', line 12

def delete(key)
  if @cache.respond_to? :delete
    @cache.delete key
  elsif @cache.respond_to? :unset
    @cache.unset key
  else
    raise NoMethodError, 'Cache must respond to :delete or :unset'
  end
end

#read(key) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/jahuty/cache/facade.rb', line 22

def read(key)
  if @cache.respond_to? :read
    @cache.read key
  elsif @cache.respond_to? :get
    @cache.get key
  else
    raise NoMethodError, 'Cache must respond to :read or :get'
  end
end

#write(key, value, expires_in: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jahuty/cache/facade.rb', line 32

def write(key, value, expires_in: nil)
  if Object.const_defined?('::ActiveSupport::Cache::Store') &&
     @cache.is_a?(::ActiveSupport::Cache::Store)
    @cache.write key, value, expires_in: expires_in, race_condition_ttl: 10
  elsif @cache.respond_to? :write
    @cache.write key, value, expires_in: expires_in
  elsif @cache.respond_to? :set
    @cache.set key, value, expires_in: expires_in
  else
    raise NoMethodError, 'Cache must respond to :write or :set'
  end
end