Class: Dalli::Client
- Inherits:
-
Object
- Object
- Dalli::Client
- Defined in:
- lib/dalli/client.rb
Instance Method Summary collapse
- #add(key, value, ttl = 0) ⇒ Object
- #append(key, value) ⇒ Object
- #cas(key, ttl = 0, &block) ⇒ Object
- #close ⇒ Object
-
#decr(key, amt, ttl = 0, default = nil) ⇒ Object
Decr subtracts the given amount from the counter on the memcached server.
- #delete(key) ⇒ Object
- #fetch(key, ttl = 0) ⇒ Object
- #flush(delay = 0) ⇒ Object
- #flush_all ⇒ Object
-
#get(key) ⇒ Object
The standard memcached instruction set.
- #get_multi(*keys) ⇒ Object
-
#incr(key, amt, ttl = 0, default = nil) ⇒ Object
Incr adds the given amount to the counter on the memcached server.
-
#initialize(servers = nil, options = {}) ⇒ Client
constructor
Dalli::Client is the main class which developers will use to interact with the memcached server.
- #prepend(key, value) ⇒ Object
- #replace(key, value, ttl = 0) ⇒ Object
- #set(key, value, ttl = 0) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(servers = nil, options = {}) ⇒ Client
Dalli::Client is the main class which developers will use to interact with the memcached server. Usage: <pre> Dalli::Client.new([‘localhost:11211:10’, ‘cache-2.example.com:11211:5’, ‘192.168.0.1:22122:5’],
:threadsafe => false, :marshal => false)
</pre> servers is an Array of “host:port:weight” where weight allows you to distribute cache unevenly. Both weight and port are optional.
Options:
:failover - if a server is down, store the value on another server. Default: true.
:threadsafe - ensure that only one thread is actively using a socket at a time. Default: true.
:marshal - ensure that the value you store is exactly what is returned. Otherwise you can see this:
set('abc', 123)
get('abc') ==> '123' (Note you set an Integer but got back a String)
Default: true.
22 23 24 25 26 27 28 29 |
# File 'lib/dalli/client.rb', line 22 def initialize(servers=nil, ={}) @ring = Dalli::Ring.new( Array(servers || env_servers).map do |s| Dalli::Server.new(s) end, ) self.extend(Dalli::Marshal) unless [:marshal] == false end |
Instance Method Details
#add(key, value, ttl = 0) ⇒ Object
72 73 74 |
# File 'lib/dalli/client.rb', line 72 def add(key, value, ttl=0) perform(:add, key, serialize(value), ttl, 0) end |
#append(key, value) ⇒ Object
84 85 86 |
# File 'lib/dalli/client.rb', line 84 def append(key, value) perform(:append, key, value.to_s) end |
#cas(key, ttl = 0, &block) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/dalli/client.rb', line 59 def cas(key, ttl=0, &block) (value, cas) = perform(:cas, key) value = (!value || value == 'Not found') ? nil : deserialize(value) if value newvalue = block.call(value) perform(:add, key, serialize(newvalue), ttl, cas) end end |
#close ⇒ Object
135 136 137 138 |
# File 'lib/dalli/client.rb', line 135 def close @ring.servers.map { |s| s.close } @ring = nil end |
#decr(key, amt, ttl = 0, default = nil) ⇒ Object
Decr subtracts the given amount from the counter on the memcached server. Amt must be a positive value.
memcached counters are unsigned and cannot hold negative values. Calling decr on a counter which is 0 will just return 0.
If default is nil, the counter must already exist or the operation will fail and will return nil. Otherwise this method will return the new value for the counter.
126 127 128 129 |
# File 'lib/dalli/client.rb', line 126 def decr(key, amt, ttl=0, default=nil) raise ArgumentError, "Positive values only: #{amt}" if amt < 0 perform(:decr, key, amt, ttl, default) end |
#delete(key) ⇒ Object
80 81 82 |
# File 'lib/dalli/client.rb', line 80 def delete(key) perform(:delete, key) end |
#fetch(key, ttl = 0) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/dalli/client.rb', line 50 def fetch(key, ttl=0) val = get(key) if val.nil? && block_given? val = yield add(key, val, ttl) end val end |
#flush(delay = 0) ⇒ Object
92 93 94 95 |
# File 'lib/dalli/client.rb', line 92 def flush(delay=0) time = -delay @ring.servers.map { |s| s.request(:flush, time += delay) } end |
#flush_all ⇒ Object
97 98 99 |
# File 'lib/dalli/client.rb', line 97 def flush_all flush(0) end |
#get(key) ⇒ Object
The standard memcached instruction set
35 36 37 38 |
# File 'lib/dalli/client.rb', line 35 def get(key) resp = perform(:get, key) (!resp || resp == 'Not found') ? nil : deserialize(resp) end |
#get_multi(*keys) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/dalli/client.rb', line 40 def get_multi(*keys) @ring.lock do keys.flatten.each do |key| perform(:getkq, key) end values = @ring.servers.inject({}) { |hash, s| hash.merge!(s.request(:noop)); hash } values.inject(values) { |memo, (k,v)| memo[k] = deserialize(v); memo } end end |
#incr(key, amt, ttl = 0, default = nil) ⇒ Object
Incr adds the given amount to the counter on the memcached server. Amt must be a positive value.
memcached counters are unsigned and cannot hold negative values. Calling decr on a counter which is 0 will just return 0.
If default is nil, the counter must already exist or the operation will fail and will return nil. Otherwise this method will return the new value for the counter.
111 112 113 114 |
# File 'lib/dalli/client.rb', line 111 def incr(key, amt, ttl=0, default=nil) raise ArgumentError, "Positive values only: #{amt}" if amt < 0 perform(:incr, key, amt, ttl, default) end |
#prepend(key, value) ⇒ Object
88 89 90 |
# File 'lib/dalli/client.rb', line 88 def prepend(key, value) perform(:prepend, key, value.to_s) end |
#replace(key, value, ttl = 0) ⇒ Object
76 77 78 |
# File 'lib/dalli/client.rb', line 76 def replace(key, value, ttl=0) perform(:replace, key, serialize(value), ttl) end |
#set(key, value, ttl = 0) ⇒ Object
68 69 70 |
# File 'lib/dalli/client.rb', line 68 def set(key, value, ttl=0) perform(:set, key, serialize(value), ttl) end |
#stats ⇒ Object
131 132 133 |
# File 'lib/dalli/client.rb', line 131 def stats @ring.servers.inject({}) { |memo, s| memo["#{s.hostname}:#{s.port}"] = s.request(:stats); memo } end |