class ActiveSupport::Cache::RedisStore

Public Class Methods

new(*addresses) click to toggle source

Instantiate the store.

Example:

RedisStore.new
  # => host: localhost,   port: 6379,  db: 0

RedisStore.new "example.com"
  # => host: example.com, port: 6379,  db: 0

RedisStore.new "example.com:23682"
  # => host: example.com, port: 23682, db: 0

RedisStore.new "example.com:23682/1"
  # => host: example.com, port: 23682, db: 1

RedisStore.new "example.com:23682/1/theplaylist"
  # => host: example.com, port: 23682, db: 1, namespace: theplaylist

RedisStore.new "localhost:6379/0", "localhost:6380/0"
  # => instantiate a cluster
Calls superclass method
# File lib/active_support/cache/redis_store.rb, line 24
def initialize(*addresses)
  @data = ::Redis::Store::Factory.create(addresses)
  super()
end

Public Instance Methods

clear() click to toggle source

Clear all the data from the store.

# File lib/active_support/cache/redis_store.rb, line 118
def clear
  @data.flushdb
end
decrement(key, amount = 1) click to toggle source

Decrement a key in the store

If the key doesn’t exist it will be initialized on 0. If the key exist but it isn’t a Fixnum it will be initialized on 0.

Example:

We have two objects in cache:
  counter # => 23
  rabbit  # => #<Rabbit:0x5eee6c>

cache.decrement "counter"
cache.read "counter", :raw => true      # => "22"

cache.decrement "counter", 2
cache.read "counter", :raw => true      # => "20"

cache.decrement "a counter"
cache.read "a counter", :raw => true    # => "-1"

cache.decrement "rabbit"
cache.read "rabbit", :raw => true       # => "-1"
# File lib/active_support/cache/redis_store.rb, line 113
def decrement(key, amount = 1)
  @data.decrby key, amount
end
delete_matched(matcher, options = nil) click to toggle source

Delete objects for matched keys.

Example:

cache.del_matched "rab*"
# File lib/active_support/cache/redis_store.rb, line 37
def delete_matched(matcher, options = nil)
  matcher = key_matcher(matcher, options)
  begin
    !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
  rescue Errno::ECONNREFUSED => e
    false
  end
end
increment(key, amount = 1) click to toggle source

Increment a key in the store.

If the key doesn’t exist it will be initialized on 0. If the key exist but it isn’t a Fixnum it will be initialized on 0.

Example:

We have two objects in cache:
  counter # => 23
  rabbit  # => #<Rabbit:0x5eee6c>

cache.increment "counter"
cache.read "counter", :raw => true      # => "24"

cache.increment "counter", 6
cache.read "counter", :raw => true      # => "30"

cache.increment "a counter"
cache.read "a counter", :raw => true    # => "1"

cache.increment "rabbit"
cache.read "rabbit", :raw => true       # => "1"
# File lib/active_support/cache/redis_store.rb, line 88
def increment(key, amount = 1)
  @data.incrby key, amount
end
read(name, options=nil) click to toggle source
# File lib/active_support/cache/redis_store.rb, line 46
def read(name, options=nil)
  read_multi(name)[name]
end
read_multi(*names) click to toggle source

Reads multiple keys from the cache using a single call to the servers for all keys. Options can be passed in the last argument.

Example:

cache.read_multi "rabbit", "white-rabbit"
cache.read_multi "rabbit", "white-rabbit", :raw => true
# File lib/active_support/cache/redis_store.rb, line 56
def read_multi(*names)
  values = @data.mget(*names)

  # Remove the options hash before mapping keys to values
  names.extract_options!

  result = Hash[names.zip(values)]
  result.reject!{ |k,v| v.nil? }
  result
end
reconnect() click to toggle source

Force client reconnection, useful Unicorn deployed apps.

# File lib/active_support/cache/redis_store.rb, line 127
def reconnect
  @data.reconnect
end
stats() click to toggle source
# File lib/active_support/cache/redis_store.rb, line 122
def stats
  @data.info
end
write(name, value, options = nil) click to toggle source
# File lib/active_support/cache/redis_store.rb, line 29
def write(name, value, options = nil)
  write_entry(name, value, options)
end

Protected Instance Methods

delete_entry(key, options) click to toggle source

Implement the ActiveSupport::Cache#delete_entry

It’s really needed and use

# File lib/active_support/cache/redis_store.rb, line 150
def delete_entry(key, options)
  @data.del key
rescue Errno::ECONNREFUSED => e
  false
end
key_matcher(pattern, options) click to toggle source

Add the namespace defined in the options to a pattern designed to match keys.

This implementation is __different__ than ActiveSupport: __it doesn’t accept Regular expressions__, because the Redis matcher is designed only for strings with wildcards.

# File lib/active_support/cache/redis_store.rb, line 162
def key_matcher(pattern, options)
  prefix = options[:namespace].is_a?(Proc) ? options[:namespace].call : options[:namespace]
  if prefix
    raise "Regexps aren't supported, please use string with wildcards." if pattern.is_a?(Regexp)
    "#{prefix}:#{pattern}"
  else
    pattern
  end
end
read_entry(key, options) click to toggle source
# File lib/active_support/cache/redis_store.rb, line 139
def read_entry(key, options)
  @data.get key, options
rescue Errno::ECONNREFUSED => e
  nil
end
write_entry(key, entry, options) click to toggle source
# File lib/active_support/cache/redis_store.rb, line 132
def write_entry(key, entry, options)
  method = options && options[:unless_exist] ? :setnx : :set
  @data.send method, key, entry, options
rescue Errno::ECONNREFUSED => e
  false
end