Class: Ramaze::Cache::Redis
- Inherits:
-
Object
- Object
- Ramaze::Cache::Redis
- Includes:
- Cache::API, Innate::Traited
- Defined in:
- lib/ramaze/cache/redis.rb
Overview
The Redis cache is a cache driver for Redis (http://redis.io/). Redis is a key/value store similar to Memcached but with the ability to flush data to a file among various other features.
The usage of this cache is very similar to the Memcache driver. You load it by simply specifying the class:
Ramaze::Cache..session = Ramaze::Cache::Redis
If you want to specify custom options you can do so by calling Redis.using on the class:
Ramaze::Cache.options.session = Ramaze::Cache::Redis.using(...)
Class Attribute Summary (collapse)
Instance Attribute Summary (collapse)
-
- (Object) options
Hash containing all the default options merged with the user specified ones.
Class Method Summary (collapse)
-
+ (Object) using(options = {})
Creates a new instance of the cache class and merges the default options with the custom ones.
Instance Method Summary (collapse)
-
- (Object) cache_clear
Clears the entire cache.
-
- (Object) cache_delete(*keys)
Removes a number of keys from the cache.
-
- (Mixed) cache_fetch(key, default = nil)
Retrieves the value of the given key.
-
- (Object) cache_setup(hostname, username, appname, cachename)
Prepares the cache by setting up the namespace and loading Redis.
-
- (Object) cache_store(key, value, ttl = nil, options = {})
Stores a new value under the given key.
-
- (Redis) initialize(options = {})
constructor
Creates a new instance of the cache and merges the options if they haven't already been set.
-
- (Object) namespaced_key(key)
Methods included from Innate::Traited
#ancestral_trait, #ancestral_trait_values, #class_trait, #each_ancestral_trait, included, #trait
Constructor Details
- (Redis) initialize(options = {})
Creates a new instance of the cache and merges the options if they haven't already been set.
81 82 83 84 85 86 |
# File 'lib/ramaze/cache/redis.rb', line 81 def initialize( = {}) self.class. ||= Ramaze::Cache::Redis.trait[:default].merge() @options = .merge(self.class.) end |
Class Attribute Details
+ (Object) options
47 48 49 |
# File 'lib/ramaze/cache/redis.rb', line 47 def @options end |
Instance Attribute Details
- (Object) options
Hash containing all the default options merged with the user specified ones.
44 45 46 |
# File 'lib/ramaze/cache/redis.rb', line 44 def @options end |
Class Method Details
+ (Object) using(options = {})
Creates a new instance of the cache class and merges the default options with the custom ones.
Using this method you can specify custom options for various caches. For example, the Redis cache for your sessions could be located at server #1 while a custom cache is located on server #2.
66 67 68 69 |
# File 'lib/ramaze/cache/redis.rb', line 66 def using( = {}) merged = Ramaze::Cache::Redis.trait[:default].merge() Class.new(self) { @options = merged } end |
Instance Method Details
- (Object) cache_clear
Clears the entire cache.
115 116 117 |
# File 'lib/ramaze/cache/redis.rb', line 115 def cache_clear @client.flushall end |
- (Object) cache_delete(*keys)
Removes a number of keys from the cache.
125 126 127 |
# File 'lib/ramaze/cache/redis.rb', line 125 def cache_delete(*keys) @client.del(*keys.map{|key| namespaced_key(key) }) end |
- (Mixed) cache_fetch(key, default = nil)
Retrieves the value of the given key. If no value could be retrieved the default value (set to nil by default) will be returned instead.
138 139 140 141 |
# File 'lib/ramaze/cache/redis.rb', line 138 def cache_fetch(key, default = nil) value = @client.get(namespaced_key(key)) value.nil? ? default : ::Marshal.load(value) end |
- (Object) cache_setup(hostname, username, appname, cachename)
Prepares the cache by setting up the namespace and loading Redis.
101 102 103 104 105 106 107 |
# File 'lib/ramaze/cache/redis.rb', line 101 def cache_setup(hostname, username, appname, cachename) [:namespace] = [ 'ramaze', hostname, username, appname, cachename ].compact.join(':') @client = ::Redis.new() end |
- (Object) cache_store(key, value, ttl = nil, options = {})
Stores a new value under the given key.
153 154 155 156 157 158 159 |
# File 'lib/ramaze/cache/redis.rb', line 153 def cache_store(key, value, ttl = nil, = {}) ttl = [:ttl] || @options[:expires_in] @client.setex(namespaced_key(key), ttl, ::Marshal.dump(value)) return value end |
- (Object) namespaced_key(key)
161 162 163 |
# File 'lib/ramaze/cache/redis.rb', line 161 def namespaced_key(key) [[:namespace], key].join(':') end |