From 65e8a725cb152a90c9596860010db6455e3b6ea9 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Sat, 5 Mar 2016 14:22:15 -0500 Subject: [PATCH] Get/Set raw Now we will use raw instead of encode/decode --- lib/memcached.rb | 18 +++--- test/unit/client_get_test.rb | 118 +++++++++++++++++------------------ 2 files changed, 67 insertions(+), 69 deletions(-) diff --git a/lib/memcached.rb b/lib/memcached.rb index 3683e06..2c61f61 100644 --- a/lib/memcached.rb +++ b/lib/memcached.rb @@ -22,24 +22,28 @@ module Memcached connection.flush end - def set(key, value, ttl: 0, encode: true, flags: FLAGS) - value, flags = @codec.encode(key, value, flags) if encode + def set(key, value, ttl: 0, raw: false, flags: FLAGS) + value, flags = @codec.encode(key, value, flags) unless raw connection.set(key, value) end - def get(key) + def get(key, raw: false) value = connection.get(key) return nil unless value - value = @codec.decode(key, value, FLAGS)# if decode + value = @codec.decode(key, value, FLAGS) unless raw value end - def get_multi(keys) + def get_multi(keys, raw: false) + keys = keys.compact hash = connection.get_multi(keys) - hash.each do |key, value| - hash[key] = @codec.decode(key, value, FLAGS) + unless raw + hash.each do |key, value| + hash[key] = @codec.decode(key, value, FLAGS) + end end + hash end def connection diff --git a/test/unit/client_get_test.rb b/test/unit/client_get_test.rb index d81d281..44aaddf 100644 --- a/test/unit/client_get_test.rb +++ b/test/unit/client_get_test.rb @@ -190,68 +190,62 @@ class ClientGetTest < Minitest::Test # assert_equal({"magic_key" => 1}, @binary_protocol_cache.get(["magic_key"])) # end # -# def test_get_multi_completely_missing -# @cache.delete "#{key}_1" rescue nil -# @cache.delete "#{key}_2" rescue nil -# assert_equal( -# {}, -# @cache.get(["#{key}_1", "#{key}_2"]) -# ) -# end -# -# def test_get_multi_empty_string -# @cache.set "#{key}_1", "", 0, false -# assert_equal({"#{key}_1" => ""}, -# @cache.get(["#{key}_1"], false)) -# end -# -# def test_get_multi_coerces_string_type -# assert_nothing_raised do -# @cache.get [nil] -# end -# assert_raises(TypeError) do -# @cache.get [1] -# end -# end -# -# def test_set_and_get_unmarshalled -# @cache.set key, @value -# result = @cache.get key, false -# assert_equal @marshalled_value, result -# end -# -# def test_set_unmarshalled_and_get_unmarshalled -# @cache.set key, @marshalled_value, 0, false -# result = @cache.get key, false -# assert_equal @marshalled_value, result -# end -# -# def test_set_unmarshalled_error -# assert_raises(TypeError) do -# @cache.set key, @value, 0, false -# end -# end -# -# def test_get_multi_unmarshalled -# @cache.set "#{key}_1", "1", 0, false -# @cache.set "#{key}_2", "2", 0, false -# assert_equal( -# {"#{key}_1" => "1", "#{key}_2" => "2"}, -# @cache.get(["#{key}_1", "#{key}_2"], false) -# ) -# end -# -# def test_get_multi_mixed_marshalling -# @cache.set "#{key}_1", 1 -# @cache.set "#{key}_2", "2", 0, false -# assert_nothing_raised do -# @cache.get(["#{key}_1", "#{key}_2"], false) -# end -# assert_raise(ArgumentError, TypeError) do -# @cache.get(["#{key}_1", "#{key}_2"]) -# end -# end -# + def test_get_multi_completely_missing + assert_equal({}, @cache.get_multi(["#{key}_1", "#{key}_2"])) + end + + def test_get_multi_empty_string + @cache.set "#{key}_1", "", ttl: 0, raw: true + assert_equal({"#{key}_1" => ""}, + @cache.get_multi(["#{key}_1"], raw: true)) + end + + def test_get_multi_coerces_string_type + assert @cache.get_multi [nil] + + assert_raises(TypeError) do + @cache.get_multi [1] + end + end + + def test_set_and_get_unmarshalled + @cache.set key, @value + result = @cache.get key, raw: true + assert_equal @marshalled_value, result + end + + def test_set_unmarshalled_and_get_unmarshalled + @cache.set key, @marshalled_value, raw: true + result = @cache.get key, raw: true + assert_equal @marshalled_value, result + end + + def test_set_unmarshalled_error + assert_raises(TypeError) do + @cache.set key, @value, raw: true + end + end + + def test_get_multi_unmarshalled + @cache.set "#{key}_1", "1", raw: true + @cache.set "#{key}_2", "2", raw: true + assert_equal( + {"#{key}_1" => "1", "#{key}_2" => "2"}, + @cache.get_multi(["#{key}_1", "#{key}_2"], raw: true) + ) + end + + def test_get_multi_mixed_marshalling + @cache.set "#{key}_1", 1 + @cache.set "#{key}_2", "2", raw: true + + assert @cache.get_multi(["#{key}_1", "#{key}_2"], raw: true) + + assert_raises(ArgumentError, TypeError) do + @cache.get_multi(["#{key}_1", "#{key}_2"]) + end + end + # def test_random_distribution_is_statistically_random # cache = Memcached.new(@servers, :distribution => :random) # read_cache = Memcached.new(@servers.first)