Skip to content

Commit

Permalink
tempo
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp committed Nov 29, 2018
1 parent ce855a0 commit 1eb5db8
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 59 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
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
12 changes: 8 additions & 4 deletions lib/any_cache/adapters/dalli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,26 @@ 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)
raw = options.fetch(:raw, false)

get(key)
end

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

get_multi(*keys).tap do |res|
res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
end
Expand All @@ -99,8 +103,6 @@ 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) }
end

Expand All @@ -113,6 +115,8 @@ def write_multi(entries, **options)
# @api private
# @since 0.2.0
def fetch(key, **options, &fallback)
raw = options.fetch(:raw, false)

force_rewrite = options.fetch(:force, false)
force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)

Expand Down
14 changes: 11 additions & 3 deletions lib/any_cache/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def supported_driver?(driver)
# @api private
# @since 0.1.0
def read(key, **options)
get(key)
value = get(key)
raw = options.fetch(:raw, true)
raw ? value : AnyCache::Dumper.load(value)
end

# @param keys [Array<String>]
Expand All @@ -77,7 +79,9 @@ def read(key, **options)
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
mapped_mget(*keys)
raw = options.fetch(:raw, true)
entries = mapped_mget(*keys)
raw ? entries : AnyCache::Dumper.detransform_hash(entries)
end

# @param key [String]
Expand All @@ -88,7 +92,9 @@ def read_multi(*keys, **options)
# @api private
# @since 0.1.0
def write(key, value, **options)
raw = options.fetch(:raw, true)
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 +106,8 @@ def write(key, value, **options)
# @api private
# @since 0.3.0
def write_multi(entries, **options)
raw = options.fetch(:raw, true)
entries = AnyCache::Dumper.transform_hash(entries) unless raw
mapped_mset(entries)
end

Expand All @@ -115,7 +123,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
8 changes: 5 additions & 3 deletions lib/any_cache/adapters/redis_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def supported_driver?(driver)
# @since 0.1.0
def read(key, **options)
raw = options.fetch(:raw, true)

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

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

expires_in ? setex(key, expires_in, value, raw: raw) : set(key, value, raw: raw)
end
Expand All @@ -61,7 +62,8 @@ 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, true)
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.

2 changes: 1 addition & 1 deletion spec/features/write_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe 'Operation: #write' do
describe 'Operation: #write', :focus do
include_context 'cache store'

let(:expiration_time) { 8 } # NOTE: in seconds
Expand Down

0 comments on commit 1eb5db8

Please sign in to comment.