Skip to content

Commit

Permalink
[marshaling] fixed inconsistent AnyCache::Adapters::Dalli#read_multi …
Browse files Browse the repository at this point in the history
…key types
  • Loading branch information
0exp committed Dec 2, 2018
1 parent adce940 commit 18a677c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
14 changes: 10 additions & 4 deletions lib/any_cache/adapters/dalli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ def read_multi(*keys, **options)
raw = options.fetch(:raw, false)

entries = get_multi(*keys).tap do |res|
# TODO: returned res.keys items should have consistent types with initial keys items
# (at this moment you cant return requested symbolic keys -
# your keys will be stringifed by dalli)
res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
# NOTE:
# dalli does not return nonexistent entries
# but we want to be consistent with another cache storages
# that returns nonexistent antries as { key => nil } pair
res.merge!(Hash[(keys.map(&:to_s) - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])

# NOTE:
# dalli stringifies requred keys but we want to be consistent with symbol keys
# cuz another cache storages are already consistent
keys.each { |key| res.key?(key) ? next : (res[key] = res.delete(key.to_s)) }
end

raw ? entries : detransform_pairset(entries)
Expand Down
12 changes: 6 additions & 6 deletions spec/features/raw_non_raw_read_write_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
specify '#read_multi/#write_multi works correctly' do
# worsk with any adequate type
ruby_entries = {
'hash_ruby_obj' => { a: 1, b: 2, c: 3 },
'bool_ruby_obj' => true,
'real_ruby_obj' => SimpleRubyObject.new(1, 2, 3)
hash_ruby_obj: { a: 1, b: 2, c: 3 },
bool_ruby_obj: true,
real_ruby_obj: SimpleRubyObject.new(1, 2, 3)
}

cache_store.write_multi(ruby_entries, raw: false)
Expand Down Expand Up @@ -129,9 +129,9 @@
specify '#read_multi/#write_multi works correctly' do
# works with any adequate type
ruby_entries = {
'hash_ruby_obj' => { a: 1, b: 2, c: 3 },
'bool_ruby_obj' => true,
'real_ruby_obj' => SimpleRubyObject.new(1, 2, 3)
hash_ruby_obj: { a: 1, b: 2, c: 3 },
bool_ruby_obj: true,
real_ruby_obj: SimpleRubyObject.new(1, 2, 3)
}

cache_store.write_multi(ruby_entries, raw: true)
Expand Down

0 comments on commit 18a677c

Please sign in to comment.