Method: ActiveSupport::Cache::RedisCacheStore#delete_matched

Defined in:
activesupport/lib/active_support/cache/redis_cache_store.rb

#delete_matched(matcher, options = nil) ⇒ Object

Cache Store API implementation.

Supports Redis KEYS glob patterns:

h?llo matches hello, hallo and hxllo
h*llo matches hllo and heeeello
h[ae]llo matches hello and hallo, but not hillo
h[^e]llo matches hallo, hbllo, ... but not hello
h[a-b]llo matches hallo and hbllo

Use \ to escape special characters if you want to match them verbatim.

See redis.io/commands/KEYS for more.

Failsafe: Raises errors.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 210

def delete_matched(matcher, options = nil)
  unless String === matcher
    raise ArgumentError, "Only Redis glob strings are supported: #{matcher.inspect}"
  end
  pattern = namespace_key(matcher, options)

  instrument :delete_matched, pattern do
    redis.then do |c|
      cursor = "0"
      # Fetch keys in batches using SCAN to avoid blocking the Redis server.
      nodes = c.respond_to?(:nodes) ? c.nodes : [c]

      nodes.each do |node|
        begin
          cursor, keys = node.scan(cursor, match: pattern, count: SCAN_BATCH_SIZE)
          node.unlink(*keys) unless keys.empty?
        end until cursor == "0"
      end
    end
  end
end