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 .using() on the class:
Ramaze::Cache..session = Ramaze::Cache::Redis.using(...)
Class Attribute Summary (collapse)
-
+ (Object) options
Returns the value of attribute options.
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.
Constructor Details
- (Redis) initialize(options = {})
Creates a new instance of the cache and merges the options if they haven’t already been set.
76 77 78 79 80 81 82 |
# File 'lib/ramaze/cache/redis.rb', line 76 def initialize( = {}) self.class. ||= Ramaze::Cache::Redis.trait[:default].merge( ) = .merge(self.class.) end |
Class Attribute Details
+ (Object) options
Returns the value of attribute options
42 43 44 |
# File 'lib/ramaze/cache/redis.rb', line 42 def end |
Instance Attribute Details
- (Object) options
Hash containing all the default options merged with the user specified ones.
39 40 41 |
# File 'lib/ramaze/cache/redis.rb', line 39 def 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.
61 62 63 64 |
# File 'lib/ramaze/cache/redis.rb', line 61 def using( = {}) merged = Ramaze::Cache::Redis.trait[:default].merge() Class.new(self) { = merged } end |
Instance Method Details
- (Object) cache_clear
Clears the entire cache.
111 112 113 |
# File 'lib/ramaze/cache/redis.rb', line 111 def cache_clear @client.flushall end |
- (Object) cache_delete(*keys)
Removes a number of keys from the cache.
121 122 123 |
# File 'lib/ramaze/cache/redis.rb', line 121 def cache_delete(*keys) @client.del(*keys) 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.
134 135 136 137 |
# File 'lib/ramaze/cache/redis.rb', line 134 def cache_fetch(key, default = nil) value = @client.get(key) value.nil? ? default : value end |
- (Object) cache_setup(hostname, username, appname, cachename)
Prepares the cache by setting up the namespace and loading Redis.
97 98 99 100 101 102 103 |
# File 'lib/ramaze/cache/redis.rb', line 97 def cache_setup(hostname, username, appname, cachename) [:namespace] = [ 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.
149 150 151 152 153 154 |
# File 'lib/ramaze/cache/redis.rb', line 149 def cache_store(key, value, ttl = nil, = {}) ttl = [:ttl] || [:expires_in] @client.setex(key, ttl, value) return value end |