class Cloak::Dalli

don't extend Dalli::Client so we can confirm operations are safe before adding

Public Class Methods

new(servers = nil, key: nil, **options) click to toggle source

need to use servers = nil instead of *args for Ruby < 2.7

# File lib/cloak/dalli.rb, line 12
def initialize(servers = nil, key: nil, **options)
  @dalli = ::Dalli::Client.new(servers, options)
  create_encryptor(key)
end

Public Instance Methods

add(key, value, ttl = nil, options = nil) click to toggle source
# File lib/cloak/dalli.rb, line 38
def add(key, value, ttl = nil, options = nil)
  @dalli.add(encrypt_key(key), encrypt_value(value), ttl, options)
end
decr(key, amt = 1, ttl = nil, default = nil) click to toggle source
# File lib/cloak/dalli.rb, line 54
def decr(key, amt = 1, ttl = nil, default = nil)
  @dalli.decr(encrypt_key(key), amt, ttl, default)
end
delete(key) click to toggle source
# File lib/cloak/dalli.rb, line 46
def delete(key)
  @dalli.delete(encrypt_key(key))
end
fetch(key, ttl = nil, options = nil, &blk) click to toggle source
# File lib/cloak/dalli.rb, line 29
def fetch(key, ttl = nil, options = nil, &blk)
  wrapped_blk = proc { encrypt_value(blk.call) } if blk
  decrypt_value(@dalli.fetch(encrypt_key(key), ttl, options, &wrapped_blk))
end
get(key, options = nil) click to toggle source
# File lib/cloak/dalli.rb, line 17
def get(key, options = nil)
  decrypt_value(@dalli.get(encrypt_key(key), options))
end
get_multi(*keys) click to toggle source
# File lib/cloak/dalli.rb, line 21
def get_multi(*keys)
  res = {}
  @dalli.get_multi(*keys.map { |k| encrypt_key(k) }).each do |k, v|
    res[decrypt_key(k)] = decrypt_value(v)
  end
  res
end
incr(key, amt = 1, ttl = nil, default = nil) click to toggle source
# File lib/cloak/dalli.rb, line 50
def incr(key, amt = 1, ttl = nil, default = nil)
  @dalli.incr(encrypt_key(key), amt, ttl, default)
end
replace(key, value, ttl = nil, options = nil) click to toggle source
# File lib/cloak/dalli.rb, line 42
def replace(key, value, ttl = nil, options = nil)
  @dalli.replace(encrypt_key(key), encrypt_value(value), ttl, options)
end
set(key, value, ttl = nil, options = nil) click to toggle source
# File lib/cloak/dalli.rb, line 34
def set(key, value, ttl = nil, options = nil)
  @dalli.set(encrypt_key(key), encrypt_value(value), ttl, options)
end
touch(key, ttl = nil) click to toggle source
# File lib/cloak/dalli.rb, line 58
def touch(key, ttl = nil)
  @dalli.touch(encrypt_key(key), ttl)
end