@@ -44,9 +44,12 @@ def multi
44
44
45
45
def get ( key , options = nil )
46
46
resp = perform ( :get , key )
47
- ( !resp || resp == 'Not found' ) ? nil : deserialize ( resp , options )
47
+ ( !resp || resp == 'Not found' ) ? nil : resp
48
48
end
49
49
50
+ ##
51
+ # Fetch multiple keys efficiently.
52
+ # Returns a hash of { 'key' => 'value', 'key2' => 'value1' }
50
53
def get_multi ( *keys )
51
54
return { } if keys . empty?
52
55
options = nil
@@ -56,7 +59,7 @@ def get_multi(*keys)
56
59
perform ( :getkq , key )
57
60
end
58
61
values = ring . servers . inject ( { } ) { |hash , s | hash . merge! ( s . request ( :noop ) ) ; hash }
59
- values . inject ( values ) { |memo , ( k , v ) | memo [ k ] = deserialize ( v , options ) ; memo }
62
+ values . inject ( values ) { |memo , ( k , v ) | memo [ k ] = v ; memo }
60
63
end
61
64
end
62
65
@@ -70,29 +73,46 @@ def fetch(key, ttl=nil, options=nil)
70
73
val
71
74
end
72
75
76
+ ##
77
+ # compare and swap values using optimistic locking.
78
+ # Fetch the existing value for key.
79
+ # If it exists, yield the value to the block.
80
+ # Add the block's return value as the new value for the key.
81
+ # Add will fail if someone else changed the value.
82
+ #
83
+ # Returns:
84
+ # - nil if the key did not exist.
85
+ # - false if the value was changed by someone else.
86
+ # - true if the value was successfully updated.
73
87
def cas ( key , ttl = nil , options = nil , &block )
74
88
ttl ||= @options [ :expires_in ]
75
89
( value , cas ) = perform ( :cas , key )
76
- value = ( !value || value == 'Not found' ) ? nil : deserialize ( value , options )
90
+ value = ( !value || value == 'Not found' ) ? nil : value
77
91
if value
78
92
newvalue = block . call ( value )
79
- perform ( :add , key , serialize ( newvalue , options ) , ttl , cas , options )
93
+ perform ( :add , key , newvalue , ttl , cas , options )
80
94
end
81
95
end
82
96
83
97
def set ( key , value , ttl = nil , options = nil )
84
98
ttl ||= @options [ :expires_in ]
85
- perform ( :set , key , serialize ( value , options ) , ttl , options )
99
+ perform ( :set , key , value , ttl , options )
86
100
end
87
-
101
+
102
+ ##
103
+ # Conditionally add a key/value pair, if the key does not already exist
104
+ # on the server. Returns true if the operation succeeded.
88
105
def add ( key , value , ttl = nil , options = nil )
89
106
ttl ||= @options [ :expires_in ]
90
- perform ( :add , key , serialize ( value , options ) , ttl , 0 , options )
107
+ perform ( :add , key , value , ttl , 0 , options )
91
108
end
92
109
110
+ ##
111
+ # Conditionally add a key/value pair, only if the key already exists
112
+ # on the server. Returns true if the operation succeeded.
93
113
def replace ( key , value , ttl = nil , options = nil )
94
114
ttl ||= @options [ :expires_in ]
95
- perform ( :replace , key , serialize ( value , options ) , ttl , options )
115
+ perform ( :replace , key , value , ttl , options )
96
116
end
97
117
98
118
def delete ( key )
@@ -149,10 +169,16 @@ def decr(key, amt=1, ttl=nil, default=nil)
149
169
perform ( :decr , key , amt , ttl , default )
150
170
end
151
171
172
+ ##
173
+ # Collect the stats for each server.
174
+ # Returns a hash like { 'hostname:port' => { 'stat1' => 'value1', ... }, 'hostname2:port' => { ... } }
152
175
def stats
153
176
ring . servers . inject ( { } ) { |memo , s | memo [ "#{ s . hostname } :#{ s . port } " ] = s . request ( :stats ) ; memo }
154
177
end
155
178
179
+ ##
180
+ # Close our connection to each server.
181
+ # If you perform another operation after this, the connections will be re-established.
156
182
def close
157
183
if @ring
158
184
@ring . servers . map { |s | s . close }
@@ -171,18 +197,6 @@ def ring
171
197
)
172
198
end
173
199
174
- def serialize ( value , options )
175
- value
176
- # options && options[:raw] ? value.to_s : ::Marshal.dump(value)
177
- end
178
-
179
- def deserialize ( value , options )
180
- value
181
- # options && options[:raw] ? value : ::Marshal.load(value)
182
- # rescue TypeError
183
- # raise Dalli::DalliError, "Invalid marshalled data in memcached: #{value}"
184
- end
185
-
186
200
def env_servers
187
201
ENV [ 'MEMCACHE_SERVERS' ] ? ENV [ 'MEMCACHE_SERVERS' ] . split ( ',' ) : nil
188
202
end
0 commit comments