Skip to content

Commit

Permalink
[marshaling] adpot specs and dalli/redis/redis-store dumping interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp committed Dec 1, 2018
1 parent ce855a0 commit 9dd0507
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/any_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class AnyCache
require_relative 'any_cache/version'
require_relative 'any_cache/error'
require_relative 'any_cache/serializer'
require_relative 'any_cache/dumper'
require_relative 'any_cache/drivers'
require_relative 'any_cache/adapters'
require_relative 'any_cache/logging'
Expand Down
2 changes: 1 addition & 1 deletion lib/any_cache/adapters/active_support_dalli_store.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
# frozen_string_literal: true

module AnyCache::Adapters
# @api private
Expand Down
22 changes: 0 additions & 22 deletions lib/any_cache/adapters/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,5 @@ def cleanup(**options)
def exist?(key, **options)
raise NotImplementedError
end

# @param value [Object]
# @return [String]
#
# @see AnyCacache::Serializer.dump
#
# @api private
# @since 0.4.0
def serialize(&block)
AnyCache::Serializer.dump(block.call)
end

# @param value [String]
# @return [Object]
#
# @see AnyCache::Serializer.load
#
# @api private
# @since 0.4.0
def deserialize(&block)
AnyCache::Serializer.load(block.call)
end
end
end
24 changes: 15 additions & 9 deletions lib/any_cache/adapters/dalli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,32 @@ def supported_driver?(driver)
:flush

# @param key [String]
# @param options [Hash]
# @option raw [Boolean]
# @return [Object]
#
# @api private
# @since 0.1.0
def read(key, **options)
get(key)
raw = options.fetch(:raw, false)
value = get(key)

raw ? value : AnyCache::Dumper.load(value)
end

# @param keys [Array<String>]
# @param options [Hash]
# @option raw [Boolean]
# @return [Hash]
#
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
get_multi(*keys).tap do |res|
raw = options.fethc(:raw, false)

entries = get_multi(*keys).tap do |res|
res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
end

raw ? entires : AnyCache::Dumper.detransform_hash(entries)
end

# @param key [String]
Expand All @@ -87,7 +94,8 @@ def read_multi(*keys, **options)
# @since 0.1.0
def write(key, value, **options)
expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
raw = options.fetch(:raw, true)
raw = options.fetch(:raw, false)
value = AnyCache::Dumper.dump(value) unless raw

set(key, value, expires_in, raw: raw)
end
Expand All @@ -99,9 +107,7 @@ def write(key, value, **options)
# @api private
# @since 0.3.0
def write_multi(entries, **options)
raw = options.fetch(:raw, true)

entries.each_pair { |key, value| write(key, value, raw: raw) }
entries.each_pair { |key, value| write(key, value, **options) }
end

# @param key [String]
Expand All @@ -117,7 +123,7 @@ def fetch(key, **options, &fallback)
force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)

# NOTE: can conflict with :cache_nils Dalli::Client's config
read(key).tap { |value| return value if value } unless force_rewrite
read(key, **options).tap { |value| return value if value } unless force_rewrite

yield(key).tap { |value| write(key, value, **options) } if block_given?
end
Expand Down
16 changes: 13 additions & 3 deletions lib/any_cache/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def supported_driver?(driver)
# @api private
# @since 0.1.0
def read(key, **options)
get(key)
value = get(key)
raw = options.fetch(:raw, false)

raw ? value : AnyCache::Dumper.load(value)
end

# @param keys [Array<String>]
Expand All @@ -77,7 +80,10 @@ def read(key, **options)
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
mapped_mget(*keys)
raw = options.fetch(:raw, false)
entries = mapped_mget(*keys)

raw ? entries : AnyCache::Dumper.detransform_hash(entries)
end

# @param key [String]
Expand All @@ -88,7 +94,9 @@ def read_multi(*keys, **options)
# @api private
# @since 0.1.0
def write(key, value, **options)
raw = options.fetch(:raw, false)
expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
value = AnyCache::Dumper.dump(value) unless raw

expires_in ? setex(key, expires_in, value) : set(key, value)
end
Expand All @@ -100,6 +108,8 @@ def write(key, value, **options)
# @api private
# @since 0.3.0
def write_multi(entries, **options)
raw = options.fetch(:raw, false)
entries = AnyCache::Dumper.transform_hash(entries) unless raw
mapped_mset(entries)
end

Expand All @@ -115,7 +125,7 @@ def fetch(key, **options, &fallback)
force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)

# NOTE: think about #pipelined
read(key).tap { |value| return value if value } unless force_rewrite
read(key, **options).tap { |value| return value if value } unless force_rewrite

yield(key).tap { |value| write(key, value, **options) } if block_given?
end
Expand Down
12 changes: 8 additions & 4 deletions lib/any_cache/adapters/redis_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def supported_driver?(driver)
# @api private
# @since 0.1.0
def read(key, **options)
raw = options.fetch(:raw, true)
raw = options.fetch(:raw, false)
value = get(key, raw: raw)

get(key, raw: raw)
raw ? value : AnyCache::Dumper.load(value)
end

# @param keys [Array<String>]
Expand All @@ -49,7 +50,8 @@ def read_multi(*keys, **options)
# @since 0.1.0
def write(key, value, **options)
expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
raw = options.fetch(:raw, true)
raw = options.fetch(:raw, false)
value = AnyCache::Dumper.dump(value) unless value

expires_in ? setex(key, expires_in, value, raw: raw) : set(key, value, raw: raw)
end
Expand All @@ -61,7 +63,9 @@ def write(key, value, **options)
# @api private
# @since 0.3.0
def write_multi(entries, **options)
mset(*entries.to_a.flatten!, raw: true)
raw = options.fetch(:raw, false)
entries = AnyCache::Dumper.transform_hash(entries) unless raw
mset(*entries.to_a.flatten!, raw: raw)
end
end
end
53 changes: 53 additions & 0 deletions lib/any_cache/dumper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

# @api private
# @since 0.4.0
module AnyCache::Dumper
class << self
# @param hash [Hash]
# @return [Hash]
#
# @api private
# @since 0.4.0
def transform_hash(hash)
{}.tap do |entries|
hash.each_pair do |key, value|
entries[key] = dump(value)
end
end
end

# @param hash [Hash]
# @return [Hash]
#
# @api private
# @since 0.4.0
def detransform_hash(hash)
{}.tap do |entries|
hash.each_pair do |key, value|
entries[key] = load(value)
end
end
end

# @param value [Object]
# @return [String]
#
# @api private
# @since 0.4.0
def dump(value)
return value if value.nil?
Zlib::Deflate.deflate(Marshal.dump(value))
end

# @param value [String]
# @return [Object]
#
# @api private
# @since 0.4.0
def load(value)
return value if value.nil?
Marshal.load(Zlib::Inflate.inflate(value))
end
end
end
25 changes: 0 additions & 25 deletions lib/any_cache/serializer.rb

This file was deleted.

23 changes: 15 additions & 8 deletions spec/features/increment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include_context 'cache store'

let(:expiration_time) { 8 } # NOTE: in seconds
let(:entry) { { key: SecureRandom.hex, value: 1 } }
let(:entry) { { key: SecureRandom.hex, value: 1 } }

shared_examples 'incrementation' do
specify 'by default: decrements by 1' do
Expand All @@ -31,7 +31,14 @@
end

context 'with previously defined temporal entry' do
before { cache_store.write(entry[:key], entry[:value], expires_in: expiration_time) }
before do
cache_store.write(
entry[:key],
entry[:value],
expires_in: expiration_time,
raw: true
)
end

it_behaves_like 'incrementation'

Expand All @@ -43,10 +50,10 @@
cache_store.increment(entry[:key], 2, expires_in: expiration_time)
sleep(4) # NOTE: remaining time: 4 seconds again, current value: 4

expect(cache_store.read(entry[:key]).to_i).to eq(4)
expect(cache_store.read(entry[:key], raw: true).to_i).to eq(4)
sleep(5) # NOTE: remaining time: -1 seconds

expect(cache_store.read(entry[:key])).to eq(nil)
expect(cache_store.read(entry[:key], raw: true)).to eq(nil)
end
end

Expand All @@ -60,13 +67,13 @@
expect(new_amount).to eq(2)

sleep(expiration_time + 1) # NOTE: remaining time: -1 esconds, current value: 2
expect(cache_store.read(entry[:key]).to_i).to eq(2)
expect(cache_store.read(entry[:key], raw: true).to_i).to eq(2)
end
end
end

context 'with previously defined permanent entry' do
before { cache_store.write(entry[:key], entry[:value]) }
before { cache_store.write(entry[:key], entry[:value], raw: true) }

it_behaves_like 'incrementation'

Expand All @@ -78,10 +85,10 @@
cache_store.increment(entry[:key], 2, expires_in: expiration_time)
sleep(4) # NOTE: remaining time: 4 seconds again, current value: 4

expect(cache_store.read(entry[:key]).to_i).to eq(4)
expect(cache_store.read(entry[:key], raw: true).to_i).to eq(4)
sleep(5) # NOTE: remaining time: -1 seconds

expect(cache_store.read(entry[:key])).to eq(nil)
expect(cache_store.read(entry[:key], raw: true)).to eq(nil)
end
end

Expand Down

0 comments on commit 9dd0507

Please sign in to comment.