class Jahuty::Cache::Facade

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

Public Class Methods

new(cache) click to toggle source
# File lib/jahuty/cache/facade.rb, line 8
def initialize(cache)
  @cache = cache
end

Public Instance Methods

delete(key) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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