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
- #close ⇒ Object
- #decr(key, amt) ⇒ Object
- #delete(key) ⇒ Object
- #flush(delay = 0) ⇒ Object
- #flush_all ⇒ Object
-
#get(key) ⇒ Object
The standard memcached instruction set.
- #get_multi(*keys) ⇒ Object
- #incr(key, amt) ⇒ Object
-
#initialize(servers, 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, 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:
: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.
21 22 23 24 25 26 27 28 29 |
# File 'lib/dalli/client.rb', line 21 def initialize(servers, ={}) @ring = Dalli::Ring.new( Array(servers).map do |s| Dalli::Server.new(s) end ) @ring.threadsafe! unless [:threadsafe] == false self.extend(Dalli::Marshal) unless [:marshal] == false end |
Instance Method Details
#add(key, value, ttl = 0) ⇒ Object
54 55 56 |
# File 'lib/dalli/client.rb', line 54 def add(key, value, ttl=0) perform(:add, key, serialize(value), ttl) end |
#append(key, value) ⇒ Object
66 67 68 |
# File 'lib/dalli/client.rb', line 66 def append(key, value) perform(:append, key, value) end |
#close ⇒ Object
95 96 97 |
# File 'lib/dalli/client.rb', line 95 def close @ring.servers.map { |s| s.close } end |
#decr(key, amt) ⇒ Object
87 88 89 |
# File 'lib/dalli/client.rb', line 87 def decr(key, amt) perform(:decr, key, amt) end |
#delete(key) ⇒ Object
62 63 64 |
# File 'lib/dalli/client.rb', line 62 def delete(key) perform(:delete, key) end |
#flush(delay = 0) ⇒ Object
74 75 76 77 |
# File 'lib/dalli/client.rb', line 74 def flush(delay=0) time = -delay @ring.servers.map { |s| s.request(:flush, time += delay) } end |
#flush_all ⇒ Object
79 80 81 |
# File 'lib/dalli/client.rb', line 79 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) ⇒ Object
83 84 85 |
# File 'lib/dalli/client.rb', line 83 def incr(key, amt) perform(:incr, key, amt) end |
#prepend(key, value) ⇒ Object
70 71 72 |
# File 'lib/dalli/client.rb', line 70 def prepend(key, value) perform(:prepend, key, value) end |
#replace(key, value, ttl = 0) ⇒ Object
58 59 60 |
# File 'lib/dalli/client.rb', line 58 def replace(key, value, ttl=0) perform(:replace, key, serialize(value), ttl) end |
#set(key, value, ttl = 0) ⇒ Object
50 51 52 |
# File 'lib/dalli/client.rb', line 50 def set(key, value, ttl=0) perform(:set, key, serialize(value), ttl) end |
#stats ⇒ Object
91 92 93 |
# File 'lib/dalli/client.rb', line 91 def stats @ring.servers.inject({}) { |memo, s| memo["#{s.hostname}:#{s.port}"] = s.request(:stats); memo } end |