Class: ZeevexCluster::Coordinator::Dalli

Inherits:
BaseKeyValStore show all
Defined in:
lib/zeevex_cluster/coordinator/dalli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseKeyValStore

#with_connection_retry

Methods included from Util::Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Dalli

Returns a new instance of Dalli.



14
15
16
17
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 14

def initialize(options = {})
  super
  @client ||= ::Dalli::Client.new "#@server:#@port"
end

Class Method Details

.setupObject



5
6
7
8
9
10
11
12
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 5

def self.setup
  unless @setup
    require 'dalli'
    BaseKeyValStore.setup

    @setup = true
  end
end

Instance Method Details

#add(key, value, options = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 19

def add(key, value, options = {})
  @client.add(to_key(key), serialize_value(value, options[:raw]),
              options.fetch(:expiration, @expiration), raw: raw?)
rescue ::Dalli::DalliError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

#append(key, val, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 73

def append(key, val, options = {})
  val = serialize_value(val, options[:raw])
  key = to_key(key)
  @client.append(key, val)   ||
    @client.add(key, val, options.fetch(:expiration, @expiration), raw: true) ||
    @client.append(key, val)
rescue ::Dalli::DalliError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

#cas(key, options = {}, &block) ⇒ Object

Block is passed the current value, and returns the updated value.

Block can raise DontChange to simply exit the block without updating.

returns nil for no value returns false for failure (somebody else set) returns true for success



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 46

def cas(key, options = {}, &block)
  res = @client.cas(to_key(key), options.fetch(:expiration, @expiration), raw: raw?) do |inval|
    serialize_value(yield(deserialize_value(inval, options[:raw])), options[:raw])
  end
  case res
    when nil then nil
    when false then false
    when true then true
    else raise "Unhandled status code: #{res}"
  end
rescue ZeevexCluster::Coordinator::DontChange
  false
rescue ::Dalli::DalliError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

#delete(key, options = {}) ⇒ Object



33
34
35
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 33

def delete(key, options = {})
  @client.delete(to_key(key))
end

#get(key, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 62

def get(key, options = {})
  val = @client.get(to_key(key), raw: raw?)
  if val && !options[:raw]
    deserialize_value(val)
  else
    val
  end
rescue ::Dalli::DalliError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

#prepend(key, val, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 83

def prepend(key, val, options = {})
  val = serialize_value(val, options[:raw])
  key = to_key(key)
  @client.prepend(key, val)  ||
    @client.add(key, val, options.fetch(:expiration, @expiration), raw: true) ||
    @client.prepend(key, val)
rescue ::Dalli::DalliError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

#push_to_queue(key, object, options = {}) ⇒ Object



93
94
95
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 93

def push_to_queue(key, object, options = {})

end

#set(key, value, options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/zeevex_cluster/coordinator/dalli.rb', line 26

def set(key, value, options = {})
  @client.set(to_key(key), serialize_value(value, options[:raw]),
              options.fetch(:expiration, @expiration), raw: raw?)
rescue ::Dalli::DalliError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end