Skip to content

Commit b292dcd

Browse files
committed
More rdoc, remove serialization noops
1 parent 87d34e2 commit b292dcd

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Dalli Changelog
22
=====================
33

4+
HEAD
5+
======
6+
7+
- Minor fixes, doc updates.
8+
49
0.11.0
510
======
611

lib/dalli/client.rb

+34-20
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ def multi
4444

4545
def get(key, options=nil)
4646
resp = perform(:get, key)
47-
(!resp || resp == 'Not found') ? nil : deserialize(resp, options)
47+
(!resp || resp == 'Not found') ? nil : resp
4848
end
4949

50+
##
51+
# Fetch multiple keys efficiently.
52+
# Returns a hash of { 'key' => 'value', 'key2' => 'value1' }
5053
def get_multi(*keys)
5154
return {} if keys.empty?
5255
options = nil
@@ -56,7 +59,7 @@ def get_multi(*keys)
5659
perform(:getkq, key)
5760
end
5861
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 }
6063
end
6164
end
6265

@@ -70,29 +73,46 @@ def fetch(key, ttl=nil, options=nil)
7073
val
7174
end
7275

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.
7387
def cas(key, ttl=nil, options=nil, &block)
7488
ttl ||= @options[:expires_in]
7589
(value, cas) = perform(:cas, key)
76-
value = (!value || value == 'Not found') ? nil : deserialize(value, options)
90+
value = (!value || value == 'Not found') ? nil : value
7791
if value
7892
newvalue = block.call(value)
79-
perform(:add, key, serialize(newvalue, options), ttl, cas, options)
93+
perform(:add, key, newvalue, ttl, cas, options)
8094
end
8195
end
8296

8397
def set(key, value, ttl=nil, options=nil)
8498
ttl ||= @options[:expires_in]
85-
perform(:set, key, serialize(value, options), ttl, options)
99+
perform(:set, key, value, ttl, options)
86100
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.
88105
def add(key, value, ttl=nil, options=nil)
89106
ttl ||= @options[:expires_in]
90-
perform(:add, key, serialize(value, options), ttl, 0, options)
107+
perform(:add, key, value, ttl, 0, options)
91108
end
92109

110+
##
111+
# Conditionally add a key/value pair, only if the key already exists
112+
# on the server. Returns true if the operation succeeded.
93113
def replace(key, value, ttl=nil, options=nil)
94114
ttl ||= @options[:expires_in]
95-
perform(:replace, key, serialize(value, options), ttl, options)
115+
perform(:replace, key, value, ttl, options)
96116
end
97117

98118
def delete(key)
@@ -149,10 +169,16 @@ def decr(key, amt=1, ttl=nil, default=nil)
149169
perform(:decr, key, amt, ttl, default)
150170
end
151171

172+
##
173+
# Collect the stats for each server.
174+
# Returns a hash like { 'hostname:port' => { 'stat1' => 'value1', ... }, 'hostname2:port' => { ... } }
152175
def stats
153176
ring.servers.inject({}) { |memo, s| memo["#{s.hostname}:#{s.port}"] = s.request(:stats); memo }
154177
end
155178

179+
##
180+
# Close our connection to each server.
181+
# If you perform another operation after this, the connections will be re-established.
156182
def close
157183
if @ring
158184
@ring.servers.map { |s| s.close }
@@ -171,18 +197,6 @@ def ring
171197
)
172198
end
173199

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-
186200
def env_servers
187201
ENV['MEMCACHE_SERVERS'] ? ENV['MEMCACHE_SERVERS'].split(',') : nil
188202
end

0 commit comments

Comments
 (0)