Skip to content

Commit

Permalink
[multi-operations] simplify redis delete_matched
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp committed Sep 22, 2018
1 parent 80f38e6 commit 6d15753
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 101 deletions.
45 changes: 26 additions & 19 deletions lib/any_cache/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def supported_driver?(driver)
# @since 0.1.0
DEFAULT_INCR_DECR_AMOUNT = 1

# @return [String]
#
# @api private
# @since 0.3.0
DELETE_MATCHED_CURSOR = "0"

# @return [Integer]
#
# @api private
# @since 0.3.0
DELETE_MATCHED_BATCH_SIZE = 10

# @since 0.1.0
def_delegators :driver,
:get,
Expand All @@ -48,23 +60,6 @@ def supported_driver?(driver)
:mapped_mset,
:scan

# @return [AnyCache::Adapters::Redis::DeleteMatchedHeavy]
attr_reader :delete_matched_heavy

# @return [AnyCache::Adapters::Redis::DeleteMatchedSoftly]
attr_reader :delete_matched_softly

# @param driver [Object]
# @return [void]
#
# @api private
# @since 0.3.0
def initialize(driver)
super
@delete_matched_heavy = DeleteMatchedHeavy.new(self)
@delete_matched_softly = DeleteMatchedSoftly.new(self)
end

# @param key [String]
# @param options [Hash]
# @return [Object]
Expand Down Expand Up @@ -156,9 +151,21 @@ def delete(key, **options)
# @api private
# @since 0.3.0
def delete_matched(pattern, **options)
cursor = DELETE_MATCHED_CURSOR

case pattern
when String then delete_matched_softly.call(pattern, **options)
when Regexp then delete_matched_heavy.call(pattern, **options)
when String
loop do
cursor, keys = adapter.scan(cursor, match: pattern, count: DELETE_MATCHED_BATCH_SIZE)
del(keys)
break if cursor == DELETE_MATCHED_CURSOR
end
when Regexp
loop do
cursor, keys = adapter.scan(cursor, count: DELETE_MATCHED_BATCH_SIZE)
del(keys.grep(pattern))
break if cursor == DELETE_MATCHED_CURSOR
end
end
end

Expand Down
40 changes: 0 additions & 40 deletions lib/any_cache/adapters/redis/delete_matched_basic.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/any_cache/adapters/redis/delete_matched_heavy.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/any_cache/adapters/redis/delete_matched_softly.rb

This file was deleted.

0 comments on commit 6d15753

Please sign in to comment.