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 (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
-
#options ⇒ Object
Hash containing all the default options merged with the user specified ones.
Class Method Summary collapse
-
.using(options = {}) ⇒ Object
Creates a new instance of the cache class and merges the default options with the custom ones.
Instance Method Summary collapse
-
#cache_clear ⇒ Object
Clears the entire cache.
-
#cache_delete(*keys) ⇒ Object
Removes a number of keys from the cache.
-
#cache_fetch(key, default = nil) ⇒ Mixed
Retrieves the value of the given key.
-
#cache_setup(hostname, username, appname, cachename) ⇒ Object
Prepares the cache by setting up the namespace and loading Redis.
-
#cache_store(key, value, options = {}) ⇒ Object
Stores a new value under the given key.
-
#initialize(options = {}) ⇒ Redis
constructor
Creates a new instance of the cache and merges the options if they haven’t already been set.
Constructor Details
#initialize(options = {}) ⇒ Redis
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
.options ⇒ Object
47 48 49 |
# File 'lib/ramaze/cache/redis.rb', line 47 def @options end |
Instance Attribute Details
#options ⇒ Object
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
.using(options = {}) ⇒ Object
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
#cache_clear ⇒ Object
Clears the entire cache.
115 116 117 |
# File 'lib/ramaze/cache/redis.rb', line 115 def cache_clear @client.flushall end |
#cache_delete(*keys) ⇒ Object
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) end |
#cache_fetch(key, default = nil) ⇒ Mixed
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(key) value.nil? ? default : ::Marshal.load(value) end |
#cache_setup(hostname, username, appname, cachename) ⇒ Object
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(':') redis_connection = ::Redis.new @client = ::Redis::Namespace.new([:namespace], redis: redis_connection) end |
#cache_store(key, value, options = {}) ⇒ Object
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 = [:ttl] || @options[:expires_in] @client.setex(key, ttl, ::Marshal.dump(value)) return value end |