Skip to content

Commit

Permalink
Merge 96d917c into 7e179e0
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp committed Sep 9, 2018
2 parents 7e179e0 + 96d917c commit 0ac38fb
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/any_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def build(driver = Drivers.build(config))
# @since 0.3.0
def_loggable_delegators :adapter,
:read,
:read_multi,
:write,
:write_multi,
:delete,
:increment,
:decrement,
Expand Down
34 changes: 33 additions & 1 deletion lib/any_cache/adapters/active_support_mem_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def supported_driver?(driver)
end
end

# @return [Array]
#
# @api private
# @since 0.3.0
READ_MULTI_EMPTY_KEYS_SET = [].freeze

# @return [NilClass]
#
# @api private
Expand Down Expand Up @@ -44,7 +50,7 @@ def supported_driver?(driver)
# @since 0.2.0
def_delegators :driver, :delete, :clear

# @param key
# @param key [String]
# @param options [Hash]
# @return [Object]
#
Expand All @@ -56,6 +62,20 @@ def read(key, **options)
driver.read(key, raw: raw)
end

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

driver.read_multi(*keys, raw: raw).tap do |res|
res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
end
end

# @param key [String]
# @param value [Object]
# @option expires_in [NilClass, Integer] Time in seconds
Expand All @@ -70,6 +90,18 @@ def write(key, value, **options)
driver.write(key, value, expires_in: expires_in, raw: raw)
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def write_multi(entries, **options)
raw = options.fetch(:raw, true)

driver.write_multi(entries, expires_in: NO_EXPIRATION_TTL, raw: raw)
end

# @param key [String]
# @param amount [Integer]
# @options expires_in [Integer]
Expand Down
28 changes: 28 additions & 0 deletions lib/any_cache/adapters/active_support_naive_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def read(key, **options)
lock.with_read_lock { super }
end

# @param keys [Array<String>]
# @param options [Hash]
# @return [Hash]
#
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
lock.with_read_lock do
super.tap do |res|
res.merge!(Hash[(keys - res.keys).zip(Operation::READ_MULTI_EMPTY_KEYS_SET)])
end
end
end

# @param key [String]
# @param options [Hash]
# @return [void]
Expand Down Expand Up @@ -68,6 +82,20 @@ def write(key, value, **options)
end
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def write_multi(entries, **options)
lock.with_write_lock do
expires_in = self.class::Operation::NO_EXPIRATION_TTL

super(entries, expires_in: expires_in)
end
end

# @param key [String]
# @param amount [Integer, Float]
# @option expires_in [NilClass, Integer]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class Operation
# @since 0.1.0
extend Forwardable

# @return [Array]
#
# @api private
# @since 0.3.0
READ_MULTI_EMPTY_KEYS_SET = [].freeze

# @return [NilClass]
#
# @api private
Expand Down
34 changes: 33 additions & 1 deletion lib/any_cache/adapters/active_support_redis_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def supported_driver?(driver)
end
end

# @return [Array]
#
# @api private
# @since 0.3.0
READ_MULTI_EMPTY_KEYS_SET = [].freeze

# @since 0.1.0
def_delegators :driver, :delete, :clear

Expand All @@ -38,7 +44,7 @@ def supported_driver?(driver)
# @since 0.1.0
DEFAULT_INCR_DECR_AMOUNT = 1

# @param key
# @param key [String]
# @param options [Hash]
# @return [Object]
#
Expand All @@ -50,6 +56,20 @@ def read(key, **options)
driver.read(key, raw: raw)
end

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

driver.read_multi(*keys, raw: raw).tap do |res|
res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
end
end

# @param key [String]
# @param value [Object]
# @option expires_in [NilClass, Integer] Time in seconds
Expand All @@ -64,6 +84,18 @@ def write(key, value, **options)
driver.write(key, value, expires_in: expires_in, raw: raw)
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @sicne 0.3.0
def write_multi(entries, **options)
raw = options.fetch(:raw, true)

driver.write_multi(entries, expires_in: NO_EXPIRATION_TTL, raw: raw)
end

# @param key [String]
# @param amount [Integer, Float]
# @option expires_in [Integer]
Expand Down
20 changes: 20 additions & 0 deletions lib/any_cache/adapters/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ def read(key, **options)
raise NotImplementedError
end

# @param keys [Array<String>]
# @param options [Hash]
# @return [Hash]
#
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
raise NotImplementedError
end

# @param key [String]
# @param value [Object]
# @param options [Hash]
Expand All @@ -54,6 +64,16 @@ def write(key, value, **options)
raise NotImplementedError
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def write_multi(entries, **options)
raise NotImplementedError
end

# @param key [String]
# @param options [Hash]
# @return [void]
Expand Down
40 changes: 40 additions & 0 deletions lib/any_cache/adapters/dalli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@ def supported_driver?(driver)
# @since 0.1.0
MIN_DECRESEAD_VAL = 0

# @return [Array]
#
# @api private
# @since 0.3.0
READ_MULTI_EMPTY_KEYS_SET = [].freeze

# @since 0.1.0
def_delegators :driver,
:get,
:get_multi,
:set,
:incr,
:decr,
Expand All @@ -59,13 +66,46 @@ def read(key, **options)
get(key)
end

# @param keys [Array<String>]
# @param options [Hash]
# @return [Hash]
#
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
get_multi(*keys).tap do |res|
res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
end
end

# @param key [String]
# @param value [Object]
# @option expires_in [Integer]
# @return [void]
#
# @api private
# @since 0.1.0
def write(key, value, **options)
expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
raw = options.fetch(:raw, true)

set(key, value, expires_in, raw: raw)
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def write_multi(entries, **options)
raw = options.fetch(:raw, true)

entries.to_a.flatten.each_slice(2) do |(key, value)|
write(key, value, raw: raw)
end
end

# @param key [String]
# @param options [Hash]
# @return [void]
Expand Down
8 changes: 7 additions & 1 deletion lib/any_cache/adapters/delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class << self
#
# @api private
# @since 0.1.0
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def supported_driver?(driver)
driver.respond_to?(:read) &&
driver.respond_to?(:write) &&
Expand All @@ -20,14 +21,19 @@ def supported_driver?(driver)
driver.respond_to?(:persist) &&
driver.respond_to?(:clear) &&
driver.respond_to?(:exist?) &&
driver.respond_to?(:fetch)
driver.respond_to?(:fetch) &&
driver.respond_to?(:read_multi) &&
driver.respond_to?(:write_multi)
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
end

# @since 0.1.0
def_delegators :driver,
:read,
:read_multi,
:write,
:write_multi,
:delete,
:increment,
:decrement,
Expand Down
24 changes: 23 additions & 1 deletion lib/any_cache/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def supported_driver?(driver)
:decrby,
:pipelined,
:flushdb,
:exists
:exists,
:mapped_mget,
:mapped_mset

# @param key [String]
# @param options [Hash]
Expand All @@ -55,6 +57,16 @@ def read(key, **options)
get(key)
end

# @param keys [Array<String>]
# @param options [Hash]
# @return [Hash]
#
# @api private
# @since 0.3.0
def read_multi(*keys, **options)
mapped_mget(*keys)
end

# @param key [String]
# @param value [Object]
# @option expires_in [NilClass, Integer] Time in seconds
Expand All @@ -68,6 +80,16 @@ def write(key, value, **options)
expires_in ? setex(key, expires_in, value) : set(key, value)
end

# @param entries [Hash]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def write_multi(entries, **options)
mapped_mset(entries)
end

# @param key [String]
# @param options [Hash]
# @return [void]
Expand Down

0 comments on commit 0ac38fb

Please sign in to comment.