Class: ActiveSupport::Cache::RedisSetStore::SetOwner

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

Overview

Redis set wrapper for a given partition.

Instance Method Summary collapse

Constructor Details

#initialize(set_owner_regexp, key, redis) ⇒ SetOwner

Returns a new instance of SetOwner.



123
124
125
126
127
128
129
130
131
# File 'lib/active_support/cache/redis_set_store.rb', line 123

def initialize(set_owner_regexp, key, redis)
  @key = key
  @set = nil
  match = set_owner_regexp.match(key)
  @set = match[0] if match
  @redis = redis
  @redis_set_store = STORE if defined?(STORE)
  @redis_set_store ||= @redis
end

Instance Method Details

#convert_wildcards_to_regexObject

Redis wildcard character “*” is converted to the Regexp “.*”. All other special characters are normally-escaped.



164
165
166
167
168
# File 'lib/active_support/cache/redis_set_store.rb', line 164

def convert_wildcards_to_regex
  key = Regexp.escape(@key.to_s)
  key = key.to_s.gsub('\\*', '.*')
  key << '$'
end

#delete_matchedObject



145
146
147
148
149
150
151
152
153
# File 'lib/active_support/cache/redis_set_store.rb', line 145

def delete_matched
  return unless @set && @key

  srem_list = matched
  return if srem_list.blank?

  @redis_set_store.srem(@set, srem_list)
  @redis.del(srem_list)
end

#matchedObject



155
156
157
158
159
160
# File 'lib/active_support/cache/redis_set_store.rb', line 155

def matched
  return unless @set && @key

  matcher_regexp = Regexp.compile(convert_wildcards_to_regex)
  @redis_set_store.smembers(@set).select { |smember| matcher_regexp.match(smember) }
end

#saddObject



133
134
135
136
137
# File 'lib/active_support/cache/redis_set_store.rb', line 133

def sadd
  return unless @set && @key

  @redis_set_store.sadd(@set, @key)
end

#sremObject



139
140
141
142
143
# File 'lib/active_support/cache/redis_set_store.rb', line 139

def srem
  return unless @set && @key

  @redis_set_store.srem(@set, @key)
end