Class: ActiveSupport::Cache::RedisSetStore

Inherits:
RedisStore
  • Object
show all
Defined in:
lib/active_support/cache/redis_set_store.rb

Overview

A Rails cache implementation that is backed by redis and uses sets to track keys for rapid expiration of large numbers of keys.

Generally you would set this up in your Rails environment files:

# production.rb
Rails.application.configure do
  config.cache_store = :redis_set_store, /\Auser:\d+/
end

Defined Under Namespace

Classes: SetOwner

Instance Method Summary collapse

Constructor Details

#initialize(set_owner_regexp = nil, redis_options = {}) ⇒ RedisSetStore

Instantiate the store.

set_owner_regexp: A regular expression that identifies keys within a given

Set Owner's Redis set.

For example, assume a Site model owns cache sets.
These are defined with the cache key prefix
"site:#{site.id}". So the appropriate argument would
be: `/\Asite:\d+/`

If not provided (or `nil` is provided), the default
pattern is `/\A[^:]+:\d+/`. That is, it matches
any pattern like "type:1", assuming

redis_options: Standard redis_options for a ActiveSupport::Cache::RedisStore

Example:

RedisSetStore.new
  # => pattern: /\A[^:]+:\d+/, host: localhost,   port: 6379,  db: 0

RedisSetStore.new(/\Asite:\d+/)
  # => pattern: /\Asite:\d+/,  host: localhost,   port: 6379,  db: 0

RedisSetStore.new(/\Asite:\d+/, "example.com")
  # => pattern: /\Asite:\d+/,  host: example.com, port: 6379,  db: 0

RedisSetStore.new(/\Asite:\d+/, "example.com:23682")
  # => pattern: /\Asite:\d+/,  host: example.com, port: 23682, db: 0

RedisSetStore.new(/\Asite:\d+/, "example.com:23682/1")
  # => pattern: /\Asite:\d+/,  host: example.com, port: 23682, db: 1

See more examples at ActiveSupport::Cache::RedisStore



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_support/cache/redis_set_store.rb', line 48

def initialize(set_owner_regexp = nil, redis_options = {})
  if set_owner_regexp.is_a?(Regexp)
    @set_owner_regexp = set_owner_regexp
  else
    redis_options    = set_owner_regexp
    set_owner_regexp = nil
  end
  @set_owner_regexp = set_owner_regexp || /\A[^:]+:\d+/

  super(redis_options)
end

Instance Method Details

#delete_matched(matcher, _options = nil) ⇒ Object

Delete objects for matched keys.

Example:

cache.delete_matched("user:1:rab*")

Returns number of keys that were deleted or ‘nil` if a connection error occurs.



66
67
68
69
70
# File 'lib/active_support/cache/redis_set_store.rb', line 66

def delete_matched(matcher, _options = nil)
  SetOwner.new(@set_owner_regexp, matcher, @data).delete_matched
rescue Errno::ECONNREFUSED
  nil
end

#matched(key) ⇒ Object

Find matched keys.

Example:

cache.matched("user:1:rab*")

Returns an array of matching keys or ‘nil` if a connection error occurs.



78
79
80
81
82
# File 'lib/active_support/cache/redis_set_store.rb', line 78

def matched(key)
  SetOwner.new(@set_owner_regexp, key, @data).matched
rescue Errno::ECONNREFUSED
  []
end

#pingObject

Ping the server.

Returns “PONG” on success.



87
88
89
90
# File 'lib/active_support/cache/redis_set_store.rb', line 87

def ping
  SetOwner::STORE.ping if defined?(SetOwner::STORE)
  redis.ping
end

#redisObject

Instance of redis where the cache data is stored.

Returns a Redis instance.



95
96
97
# File 'lib/active_support/cache/redis_set_store.rb', line 95

def redis
  @data
end