Permalink
Browse files

Add support for raw values in get_multi

  • Loading branch information...
1 parent caca43b commit c22d6fc003ad25c6bca62134870a259319b1801b @mperham committed Feb 20, 2010
Showing with 36 additions and 4 deletions.
  1. +2 −1 FAQ.rdoc
  2. +4 −0 History.rdoc
  3. +8 −3 lib/memcache.rb
  4. +22 −0 test/test_mem_cache.rb
View
@@ -25,7 +25,8 @@ You can increase the timeout or disable them completely with the following confi
== Isn't Evan Weaver's memcached gem faster?
-The latest version of memcached-client is anywhere from 33% to 100% slower than memcached in various benchmarks. Keep in mind this means that 10,000 get requests take 1.8 sec instead of 1.2 seconds.
+The latest version of memcached-client is anywhere from 33% to 100% slower than memcached in various
+benchmarks. Keep in mind this means that 10,000 get requests take 1.8 sec instead of 1.2 seconds.
In practice, memcache-client is unlikely to be a bottleneck in your system but there is always going
to be an overhead to pure Ruby. memcache-client does have the advantage of built-in integration into
Rails and should work on non-MRI platforms: JRuby, MacRuby, etc.
View
@@ -1,3 +1,7 @@
+= HEAD
+
+ * Add support for raw values in get_multi
+
= 1.7.8 (2010-02-03)
* Fix issue where autofix_keys logic did not account for namespace length. (menno)
View
@@ -295,11 +295,16 @@ def fetch(key, expiry = 0, raw = false)
# cache["b"] = 2
# cache.get_multi "a", "b" # => { "a" => 1, "b" => 2 }
#
- # Note that get_multi assumes the values are marshalled.
+ # Note that get_multi assumes the values are marshalled. You can pass
+ # in :raw => true to bypass value marshalling.
+ #
+ # cache.get_multi('a', 'b', ..., :raw => true)
def get_multi(*keys)
raise MemCacheError, 'No active servers' unless active?
+ opts = keys.last.is_a?(Hash) ? keys.pop : {}
+
keys.flatten!
key_count = keys.length
cache_keys = {}
@@ -313,13 +318,13 @@ def get_multi(*keys)
end
results = {}
-
+ raw = opts[:raw] || false
server_keys.each do |server, keys_for_server|
keys_for_server_str = keys_for_server.join ' '
begin
values = cache_get_multi server, keys_for_server_str
values.each do |key, value|
- results[cache_keys[key]] = Marshal.load value
+ results[cache_keys[key]] = raw ? value : Marshal.load(value)
end
rescue IndexError => e
# Ignore this server and try the others
View
@@ -598,6 +598,28 @@ def test_get_multi
assert_equal expected.sort, values.sort
end
+ def test_get_multi_raw
+ server = FakeServer.new
+ server.socket.data.write "VALUE my_namespace:key 0 10\r\n"
+ server.socket.data.write "0123456789\r\n"
+ server.socket.data.write "VALUE my_namespace:keyb 0 10\r\n"
+ server.socket.data.write "9876543210\r\n"
+ server.socket.data.write "END\r\n"
+ server.socket.data.rewind
+
+ @cache.servers = []
+ @cache.servers << server
+
+ values = @cache.get_multi 'key', 'keyb', :raw => true
+
+ assert_equal "get my_namespace:key my_namespace:keyb\r\n",
+ server.socket.written.string
+
+ expected = { 'key' => '0123456789', 'keyb' => '9876543210' }
+
+ assert_equal expected.sort, values.sort
+ end
+
def test_get_raw
server = FakeServer.new
server.socket.data.write "VALUE my_namespace:key 0 10\r\n"

1 comment on commit c22d6fc

great, thanks!

Please sign in to comment.