class ActiveSupport::Cache::RedisCacheStore

Public Class Methods

new(*connections) click to toggle source

Creates a new RedisStore object, with the given redis server address. Each address is a valid redis url string. For example:

ActiveSupport::Cache::RedisStore.new("redis://127.0.0.1:6379/0")

Instead of addresses one can pass in a redis-like object. For example:

require 'redis' # gem install redis; uses C bindings to libredis
ActiveSupport::Cache::RedisStore.new(Redis.connect("localhost"))
Calls superclass method
# File lib/active_support/cache/redis_cache_store.rb, line 19
def initialize(*connections)
  options = connections.extract_options!
  super(options)

  raise ArgumentError, "Only a single connection must be provided" if connections.size > 1
  connection = connections.first || options

  if connection.respond_to?(:get)
    @data = connection
  elsif connection.is_a?(Symbol)
    require 'redis_factory'
    @data = RedisFactory.connect(connection)
  else
    redis_options = {}
    if connection.is_a?(String)
      redis_options[:url] = connection
    elsif connection.is_a?(Hash)
      redis_options = connection
      ActiveSupport::Cache::UNIVERSAL_OPTIONS.each{|name| redis_options.delete(name)}
    else
      raise ArgumentError, "Need a url or hash for a redis connection"
    end

    @data = ::Redis.connect(redis_options)
  end

  extend ActiveSupport::Cache::Strategy::LocalCache
end

Public Instance Methods

clear(options = nil) click to toggle source

Clear the entire cache on all redis servers. This method should be used with care when shared cache is being used.

# File lib/active_support/cache/redis_cache_store.rb, line 104
def clear(options = nil)
  @data.flushdb
end
read_multi(*names) click to toggle source

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

# File lib/active_support/cache/redis_cache_store.rb, line 50
def read_multi(*names)
  options = names.extract_options!
  options = merged_options(options)
  keys_to_names = Hash[names.map{|name| [namespaced_key(name, options), name]}]
  raw_values = @data.mget(keys_to_names.keys)
  values = {}
  raw_values.each do |key, value|
    entry = deserialize_entry(value)
    values[keys_to_names[key]] = entry.value unless entry.expired?
  end
  values
end
stats() click to toggle source

Get the statistics from the redis servers.

# File lib/active_support/cache/redis_cache_store.rb, line 109
def stats
  @data.info
end

Private Instance Methods

deserialize_entry(raw_value) click to toggle source
# File lib/active_support/cache/redis_cache_store.rb, line 147
def deserialize_entry(raw_value)
  if raw_value
    entry = Marshal.load(raw_value) rescue raw_value
    entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
  else
    nil
  end
end
serialize_entry(entry) click to toggle source
# File lib/active_support/cache/redis_cache_store.rb, line 156
def serialize_entry(entry)
  if entry
    Marshal.dump(entry)
  else
    nil
  end
end