Skip to content

Commit

Permalink
[multi-operations] redis: delete matched proto
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp committed Sep 21, 2018
1 parent b80c6c3 commit 80f38e6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/any_cache/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ def supported_driver?(driver)
:flushdb,
:exists,
:mapped_mget,
:mapped_mset
: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]
Expand Down Expand Up @@ -138,7 +156,10 @@ def delete(key, **options)
# @api private
# @since 0.3.0
def delete_matched(pattern, **options)

case pattern
when String then delete_matched_softly.call(pattern, **options)
when Regexp then delete_matched_heavy.call(pattern, **options)
end
end

# @param key [String]
Expand Down
40 changes: 40 additions & 0 deletions lib/any_cache/adapters/redis/delete_matched_basic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

class AnyCache::Adapters::Redis
class DeleteMatchedBasic
# @since 0.3.0
extend Forwardable

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

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

# @return [AnyCache::Adapters::Redis]
#
# @api private
# @since 0.3.0
attr_reader :adapter

# @param adapter [AnyCache::Adapters::Redis]
# @return [void]
#
# @api private
# @since 0.3.0
def initiaalize(adapter)
@adapter = adapter
end

# @!method delete_matched(pattern, **options)
# @param pattern [Object]
# @param options [Hash]
# @return [void]
end
end
21 changes: 21 additions & 0 deletions lib/any_cache/adapters/redis/delete_matched_heavy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class AnyCache::Adapters::Redis
class DeleteMatchedHeavy < DeleteMatchedBasic
# @param pattern [Regexp]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def delete_matched(pattern, **options)
cursor = CURSOR_LOOP_FLAG

loop do
cursor, keys = adapter.scan(cursor, count: BATCH_SIZE)
adapter.del(keys.grep(pattern))
break if cursor == CURSOR_LOOP_FLAG
end
end
end
end
21 changes: 21 additions & 0 deletions lib/any_cache/adapters/redis/delete_matched_softly.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class AnyCache::Adapters::Redis
class DeleteMatchedSoftly < DeleteMatchedBasic
# @param pattern [String]
# @param options [Hash]
# @return [void]
#
# @api private
# @since 0.3.0
def delete_matched(pattern, **options)
cursor = CURSOR_LOOP_FLAG

loop do
cursor, keys = adapter.scan(cursor, match: pattern, count: BATCH_SIZE)
adapter.del(keys)
break if cursor == CURSOR_LOOP_FLAG
end
end
end
end

0 comments on commit 80f38e6

Please sign in to comment.