Class: Spymemcached
- Inherits:
-
Object
show all
- Defined in:
- lib/spymemcached.rb,
lib/spymemcached/rails23.rb
Overview
Memcached client Spymemcached JRuby extension
Defined Under Namespace
Classes: Error, Rails23, TimeoutError
Constant Summary
collapse
- ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/n
- DEFAULT_OPTIONS =
default options for client
{
:timeout => 0.5, :binary => true,
:should_optimize => false
}
Instance Method Summary
collapse
-
#add(key, value, ttl = 0, opts = {}) ⇒ Object
-
#append(key, value) ⇒ Object
-
#cas(key, ttl = 0, opts = {}, &block) ⇒ Object
-
#decr(key, by = 1, ttl = 0, default = 0) ⇒ Object
-
#delete(key) ⇒ Object
-
#fetch(key, ttl = 0, &block) ⇒ Object
-
#flush_all ⇒ Object
(also: #flush, #clear)
-
#get(key) ⇒ Object
(also: #[])
-
#get_multi(*keys) ⇒ Object
-
#incr(key, by = 1, ttl = 0, default = 1) ⇒ Object
-
#initialize(servers = nil, options = {}) ⇒ Spymemcached
constructor
Accepts a list of servers
and a list of options
.
-
#prepend(key, value) ⇒ Object
-
#rails23 ⇒ Object
compatible api with Rails 2.3 MemcacheStore ActionController::Base.cache_store = :mem_cache_store, Spymemcached.new(servers).rails23.
-
#replace(key, value, ttl = 0, opts = {}) ⇒ Object
-
#set(key, value, ttl = 0, opts = {}) ⇒ Object
(also: #[]=)
-
#shutdown ⇒ Object
-
#stats ⇒ Object
-
#touch(key, ttl = 0) ⇒ Object
-
#version ⇒ Object
Constructor Details
#initialize(servers = nil, options = {}) ⇒ Spymemcached
Accepts a list of servers
and a list of options
. The servers
can be either strings of the form “hostname:port” or one string format as “<hostname>:<port>,<hostname>:<port>”. Use KetamaConnectionFactory as Spymemcached MemcachedClient connection factory. See Spymemcached document for details. Different with Ruby memcache clients (e.g. Dalli), there is no raw option for operations.
Valid options
are:
[:namespace] Prepends this value to all keys added or retrieved. Default to nil.
[:timeout] Time to use as the socket read timeout, seconds. Default to 0.5 sec.
[:binary] Talks binary protocol with Memcached server. Default to true.
[:should_optimize] If true, Spymemcached low-level optimization is in effect. Default to false.
Logger: see Spymemcached for how to turn on detail log
37
38
39
40
41
42
43
44
45
|
# File 'lib/spymemcached.rb', line 37
def initialize(servers=nil, options={})
@servers, @options = Array(servers).join(','), DEFAULT_OPTIONS.merge(options)
@servers = 'localhost:11211' if @servers.empty?
@client = SpymemcachedAdapter.new(@servers, @options)
@namespace = if @options[:namespace]
@options[:namespace].is_a?(Proc) ? @options[:namespace] : lambda { @options[:namespace] }
end
at_exit { shutdown }
end
|
Instance Method Details
#add(key, value, ttl = 0, opts = {}) ⇒ Object
66
67
68
|
# File 'lib/spymemcached.rb', line 66
def add(key, value, ttl=0, opts={})
@client.add(encode(key), value, ttl, opts[:raw])
end
|
#append(key, value) ⇒ Object
95
96
97
|
# File 'lib/spymemcached.rb', line 95
def append(key, value)
@client.append(encode(key), value.to_s)
end
|
#cas(key, ttl = 0, opts = {}, &block) ⇒ Object
75
76
77
|
# File 'lib/spymemcached.rb', line 75
def cas(key, ttl=0, opts={}, &block)
@client.cas(encode(key), ttl, opts[:raw], &block)
end
|
#decr(key, by = 1, ttl = 0, default = 0) ⇒ Object
91
92
93
|
# File 'lib/spymemcached.rb', line 91
def decr(key, by=1, ttl=0, default=0)
@client.decr(encode(key), by, ttl, default)
end
|
#delete(key) ⇒ Object
83
84
85
|
# File 'lib/spymemcached.rb', line 83
def delete(key)
@client.delete(encode(key))
end
|
#fetch(key, ttl = 0, &block) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/spymemcached.rb', line 47
def fetch(key, ttl=0, &block)
val = get(key)
if val.nil? && block_given?
val = yield
add(key, val, ttl)
end
val
end
|
#flush_all ⇒ Object
Also known as:
flush, clear
115
116
117
|
# File 'lib/spymemcached.rb', line 115
def flush_all
@client.flush_all
end
|
#get(key) ⇒ Object
Also known as:
[]
56
57
58
|
# File 'lib/spymemcached.rb', line 56
def get(key)
@client.get(encode(key))
end
|
#get_multi(*keys) ⇒ Object
61
62
63
64
|
# File 'lib/spymemcached.rb', line 61
def get_multi(*keys)
key_map = Hash[keys.flatten.compact.map {|k| [encode(k), k]}]
Hash[@client.get_multi(key_map.keys).map {|k, v| [key_map[k], v]}]
end
|
#incr(key, by = 1, ttl = 0, default = 1) ⇒ Object
87
88
89
|
# File 'lib/spymemcached.rb', line 87
def incr(key, by=1, ttl=0, default=1)
@client.incr(encode(key), by, ttl, default)
end
|
#prepend(key, value) ⇒ Object
99
100
101
|
# File 'lib/spymemcached.rb', line 99
def prepend(key, value)
@client.prepend(encode(key), value.to_s)
end
|
#rails23 ⇒ Object
compatible api with Rails 2.3 MemcacheStore ActionController::Base.cache_store = :mem_cache_store, Spymemcached.new(servers).rails23
127
128
129
130
|
# File 'lib/spymemcached.rb', line 127
def rails23
require 'spymemcached/rails23'
Rails23.new(self)
end
|
#replace(key, value, ttl = 0, opts = {}) ⇒ Object
79
80
81
|
# File 'lib/spymemcached.rb', line 79
def replace(key, value, ttl=0, opts={})
@client.replace(encode(key), value, ttl, opts[:raw])
end
|
#set(key, value, ttl = 0, opts = {}) ⇒ Object
Also known as:
[]=
70
71
72
|
# File 'lib/spymemcached.rb', line 70
def set(key, value, ttl=0, opts={})
@client.set(encode(key), value, ttl, opts[:raw])
end
|
#shutdown ⇒ Object
121
122
123
|
# File 'lib/spymemcached.rb', line 121
def shutdown
@client.shutdown
end
|
#stats ⇒ Object
107
108
109
|
# File 'lib/spymemcached.rb', line 107
def stats
@client.stats
end
|
#touch(key, ttl = 0) ⇒ Object
103
104
105
|
# File 'lib/spymemcached.rb', line 103
def touch(key, ttl=0)
@client.touch(encode(key), ttl)
end
|
#version ⇒ Object
111
112
113
|
# File 'lib/spymemcached.rb', line 111
def version
@client.version
end
|