Class: ZeevexCluster::Coordinator::Memcached

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

Returns a new instance of Memcached.



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

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

Class Method Details

.setupObject



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

def self.setup
  unless @setup
    require 'memcache'
    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/memcached.rb', line 19

def add(key, value, options = {})
  status( @client.add(to_key(key), serialize_value(value, options[:raw]),
                      options.fetch(:expiration, @expiration), raw?) ) == STORED
rescue MemCache::MemCacheError
  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/memcached.rb', line 73

def append(key, val, options = {})
  val = serialize_value(val, options[:raw])
  key = to_key(key)
  status( @client.append(key, val) ) == STORED  ||
      status( @client.add(key, val, options.fetch(:expiration, @expiration), true) ) == STORED ||
      status( @client.append(key, val) ) == STORED
rescue MemCache::MemCacheError
  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/memcached.rb', line 46

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

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



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

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

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



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

def get(key, options = {})
  val = @client.get(to_key(key), raw?)
  if val && !options[:raw]
    deserialize_value(val)
  else
    val
  end
rescue MemCache::MemCacheError
  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/memcached.rb', line 83

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

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



93
94
95
# File 'lib/zeevex_cluster/coordinator/memcached.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/memcached.rb', line 26

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