Skip to content

Commit

Permalink
Allow to configure the codec (arthurnn#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Jean Boussier <[email protected]>
  • Loading branch information
casperisfine and byroot authored Apr 8, 2021
1 parent d0e8d7e commit 2915fb9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/memcached/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Client
def initialize(config = "localhost:11211", options = {})
options = DEFAULTS.merge(options)

@codec = Memcached::MarshalCodec
@codec = options.delete(:codec) || Memcached::MarshalCodec
@default_ttl = options.delete(:ttl) || 0
@prefix = options.delete(:prefix_key)
@credentials = options.delete(:credentials)
Expand Down
25 changes: 25 additions & 0 deletions test/unit/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ def test_consistent_hashing
assert(failed < keys.size / 3, "#{failed} failed out of #{keys.size}")
end

module YAMLCodec
extend self

FLAG = Memcached::Client::FLAG_ENCODED

def encode(key, value, flags)
[ YAML.dump(value), flags | FLAG ]
end

def decode(key, value, flags)
if (flags & FLAG) != 0
YAML.load(value)
else
value
end
end
end

def test_custom_codec
cache = Memcached::Client.new(@servers, codec: YAMLCodec)
cache.set("yaml", :symbol)
assert_equal :symbol, cache.get("yaml")
assert_equal YAML.dump(:symbol), cache.get("yaml", raw: true)
end

# Touch command

def test_touch_missing_key
Expand Down

0 comments on commit 2915fb9

Please sign in to comment.