Class: ActiveSupport::Cache::RedisCache

Inherits:
Store
  • Object
show all
Defined in:
lib/redis_cache.rb

Overview

A cache store implementation which stores data in Redis:

This is currently the most popular cache store for production websites.

Special features:

  • Clustering and load balancing. One can specify multiple redis servers, and RedisStore will load balance between all available servers. If a server goes down, then RedisStore will ignore it until it goes back online.

  • Time-based expiry support. See #write and the :expires_in option.

  • Per-request in memory cache for all communication with the Redis server(s).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*addresses) ⇒ RedisCache

Creates a new RedisCache object, with the given redis server addresses. Each address is either a host name, or a host-with-port string in the form of “redis://host_name:port”. For example:

ActiveSupport::Cache::RedisCache.new("localhost", "server-downstairs.localnetwork:8229")

If no addresses are specified, then MemCacheStore will connect to localhost port 11211 (the default memcached port).

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

require 'redis' # gem install redis; 
ActiveSupport::Cache::RedisCache.new(Redis.connect(:url => "redis://localhost:6380/1"))


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/redis_cache.rb', line 39

def initialize(*addresses)
  if addresses.empty?
    @data = Redis.connect
  elsif addresses.size == 1
    @data = Redis.connect :url => "redis://#{addresses.first}"
  else
    @data = self.class.build_redis_cache(*addresses)
  end

  extend Strategy::LocalCache
end

Class Method Details

.build_redis_cache(*addresses) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/redis_cache.rb', line 18

def self.build_redis_cache(*addresses)
  addresses = addresses.flatten
  options = addresses.extract_options!
  addresses = ["localhost"] if addresses.empty?
  
  #Redis.connect(address, options)
end

Instance Method Details

#clearObject



101
102
103
# File 'lib/redis_cache.rb', line 101

def clear
  @data.flush_all
end

#decrement(key, amount = 1) ⇒ Object

:nodoc:



89
90
91
92
# File 'lib/redis_cache.rb', line 89

def decrement(key, amount = 1) # :nodoc:
  log("decrement", key, amount)
  @data.decr(key, amount)
end

#delete(key, options = nil) ⇒ Object

:nodoc:



75
76
77
78
# File 'lib/redis_cache.rb', line 75

def delete(key, options = nil) # :nodoc:
  super
  response = @data.del(key)
end

#delete_matched(matcher, options = nil) ⇒ Object

:nodoc:



94
95
96
97
98
99
# File 'lib/redis_cache.rb', line 94

def delete_matched(matcher, options = nil) # :nodoc:
  super
  t = @data.keys(matcher)
  @data.del(*t)
  @data.keys(matcher).empty?
end

#exist?(key, options = nil) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


80
81
82
# File 'lib/redis_cache.rb', line 80

def exist?(key, options = nil) # :nodoc:
  @data.exists(key)
end

#increment(key, amount = 1) ⇒ Object

:nodoc:



84
85
86
87
# File 'lib/redis_cache.rb', line 84

def increment(key, amount = 1) # :nodoc:
  log("incrementing", key, amount)
  @data.incr(key, amount)
end

#read(key, options = nil) ⇒ Object

:nodoc:



56
57
58
59
60
# File 'lib/redis_cache.rb', line 56

def read(key, options = nil) # :nodoc:
  super
  result = @data.get(key)
  result = Marshal.load result if unmarshal?(result, options)
end

#read_multi(*keys) ⇒ Object

Reads multiple keys from the cache.



52
53
54
# File 'lib/redis_cache.rb', line 52

def read_multi(*keys)
  @data.mget(*keys).map{|a| Marshal.load(a)  }
end

#statsObject



105
106
107
# File 'lib/redis_cache.rb', line 105

def stats
  @data.stats
end

#write(key, value, options = nil) ⇒ Object

Writes a value to the cache.

Possible options:

  • :unless_exist - set to true if you don’t want to update the cache if the key is already set.

  • :expires_in - the number of seconds that this value may stay in the cache. See ActiveSupport::Cache::Store#write for an example.



69
70
71
72
73
# File 'lib/redis_cache.rb', line 69

def write(key, value, options = nil)
  super
  val = raw?(options) ? value : Marshal.dump(value)
  @data.set(key, val)
end