Class: ZeevexCluster::Coordinator::Redis

Inherits:
BaseKeyValStore show all
Defined in:
lib/zeevex_cluster/coordinator/redis.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 = {}) ⇒ Redis

Returns a new instance of Redis.



13
14
15
16
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 13

def initialize(options = {})
  super
  @client ||= ::Redis.new :host => @server, :port => @port
end

Class Method Details

.setupObject



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

def self.setup
  unless @setup
    require 'redis'
    BaseKeyValStore.setup
    @setup = true
  end
end

Instance Method Details

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 18

def add(key, value, options = {})
  if @client.setnx(to_key(key), serialize_value(value, is_raw?(options)))
    @client.expire to_key(key), options.fetch(:expiration, @expiration)
    true
  else
    false
  end
rescue ::Redis::CannotConnectError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

#cas(key, options = {}) ⇒ 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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 52

def cas(key, options = {})
  key = to_key(key)
  @client.unwatch
  @client.watch key
  orig_val = @client.get key
  return nil if orig_val.nil?

  expiration = options.fetch(:expiration, @expiration)

  newval = serialize_value(yield(deserialize_value(orig_val, is_raw?(options))), is_raw?(options))
  res = @client.multi do
    if expiration
      @client.setex key, expiration, newval
    else
      @client.set key, newval
    end
  end
  @client.unwatch
  case res
    when nil then false
    when Array then true
    else raise "Unhandled return value from multi - #{res.inspect}"
  end
rescue ZeevexCluster::Coordinator::DontChange => e
  false
rescue ::Redis::CannotConnectError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

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



37
38
39
40
41
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 37

def delete(key, options = {})
  @client.del(to_key(key)) == 1
rescue ::Redis::CannotConnectError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

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



81
82
83
84
85
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 81

def get(key, options = {})
  deserialize_value(@client.get(to_key(key)), is_raw?(options))
rescue ::Redis::CannotConnectError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end

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



29
30
31
32
33
34
35
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 29

def set(key, value, options = {})
  status( @client.setex(to_key(key),
                        options.fetch(:expiration, @expiration),
                        serialize_value(value, is_raw?(options))) ) == STATUS_OK
rescue ::Redis::CannotConnectError
  raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $!
end