Class: ActiveSupport::Cache::RedisCache
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::RedisCache
- 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_inoption. -
Per-request in memory cache for all communication with the Redis server(s).
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
-
#decrement(key, amount = 1) ⇒ Object
:nodoc:.
-
#delete(key, options = nil) ⇒ Object
:nodoc:.
-
#delete_matched(matcher, options = nil) ⇒ Object
:nodoc:.
-
#exist?(key, options = nil) ⇒ Boolean
:nodoc:.
-
#increment(key, amount = 1) ⇒ Object
:nodoc:.
-
#initialize(*addresses) ⇒ RedisCache
constructor
Creates a new RedisCache object, with the given redis server addresses.
-
#read(key, options = nil) ⇒ Object
:nodoc:.
-
#read_multi(*keys) ⇒ Object
Reads multiple keys from the cache.
- #stats ⇒ Object
-
#write(key, value, options = nil) ⇒ Object
Writes a value to the cache.
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 = addresses. addresses = ["localhost"] if addresses.empty? #Redis.connect(address, options) end |
Instance Method Details
#clear ⇒ Object
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, = 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, = nil) # :nodoc: super t = @data.keys(matcher) @data.del(*t) @data.keys(matcher).empty? end |
#exist?(key, options = nil) ⇒ Boolean
:nodoc:
80 81 82 |
# File 'lib/redis_cache.rb', line 80 def exist?(key, = 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, = nil) # :nodoc: super result = @data.get(key) result = Marshal.load result if unmarshal?(result, ) 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 |
#stats ⇒ Object
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, = nil) super val = raw?() ? value : Marshal.dump(value) @data.set(key, val) end |