Class: Cloak::Dalli

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/cloak/dalli.rb

Overview

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

Constant Summary

Constants included from Utils

Utils::HLL_ELEMENT_NONCE, Utils::KEY_NONCE, Utils::MEMBER_NONCE

Instance Method Summary collapse

Constructor Details

#initialize(servers = nil, key: nil, **options) ⇒ Dalli

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



12
13
14
15
# File 'lib/cloak/dalli.rb', line 12

def initialize(servers = nil, key: nil, **options)
  @dalli = ::Dalli::Client.new(servers, options)
  create_encryptor(key)
end

Instance Method Details

#add(key, value, ttl = nil, options = nil) ⇒ Object



38
39
40
# 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) ⇒ Object



54
55
56
# 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) ⇒ Object



46
47
48
# File 'lib/cloak/dalli.rb', line 46

def delete(key)
  @dalli.delete(encrypt_key(key))
end

#fetch(key, ttl = nil, options = nil, &blk) ⇒ Object



29
30
31
32
# 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) ⇒ Object



17
18
19
# File 'lib/cloak/dalli.rb', line 17

def get(key, options = nil)
  decrypt_value(@dalli.get(encrypt_key(key), options))
end

#get_multi(*keys) ⇒ Object



21
22
23
24
25
26
27
# 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) ⇒ Object



50
51
52
# 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) ⇒ Object



42
43
44
# 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) ⇒ Object



34
35
36
# 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) ⇒ Object



58
59
60
# File 'lib/cloak/dalli.rb', line 58

def touch(key, ttl = nil)
  @dalli.touch(encrypt_key(key), ttl)
end