Class: Ramaze::Cache::MemCache
- Inherits:
-
Object
- Object
- Ramaze::Cache::MemCache
- Includes:
- Cache::API, Innate::Traited
- Defined in:
- lib/ramaze/cache/memcache.rb
Overview
Cache driver for the Memcache storage engine. Memcache is a key/value store that’s extremely useful for caching data such as views or API responses. More inforamtion about Memcache can be found on it’s website: http://memcached.org/.
Note that this cache driver requires the Dalli gem rather than Memcache Client. The reason for this is that the Memcache client hasn’t been updated in over a year and Memcache has changed quite a bit. Dalli is also supposed to be faster and better coded. This cache driver will also try to load the kgio Gem if it’s installed, if it’s not it will just continue to operate but you won’t get the nice speed boost.
This driver works similar to Ramaze::Cache::Sequel in that it allows you to specify instance specific options uisng the using() method:
Ramaze::Cache..session = Ramaze::Cache::Memcache.using(:compression => false)
All options sent to the using() method will be sent to Dalli.
Constant Summary
- MAX_TTL =
The maximum Time To Live that can be used in Memcache
2592000
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 = {})
This method will create a subclass of Ramaze::Cache::MemCache with all the custom options set.
Instance Method Summary (collapse)
-
- (Object) cache_clear
Removes all items from the cache.
-
- (Object) cache_delete(*keys)
Removes the specified keys from the cache.
-
- (Mixed) cache_fetch(key, default = nil)
Fetches the specified key from the cache.
-
- (Object) cache_setup(hostname, username, appname, cachename)
Prepares the cache by creating the namespace and an instance of a Dalli client.
-
- (Mixed) cache_store(key, value, ttl = nil, options = {})
Sets the given key to the specified value.
-
- (MemCache) initialize(options = {})
constructor
Creates a new instance of the cache class.
Constructor Details
- (MemCache) initialize(options = {})
Creates a new instance of the cache class.
101 102 103 104 105 106 107 |
# File 'lib/ramaze/cache/memcache.rb', line 101 def initialize( = {}) self.class. ||= Ramaze::Cache::MemCache.trait[:default].merge( ) = .merge(self.class.) end |
Class Attribute Details
+ (Object) options
Returns the value of attribute options
59 60 61 |
# File 'lib/ramaze/cache/memcache.rb', line 59 def end |
Instance Attribute Details
- (Object) options
Hash containing all the default options merged with the user specified ones
56 57 58 |
# File 'lib/ramaze/cache/memcache.rb', line 56 def end |
Class Method Details
+ (Object) using(options = {})
This method will create a subclass of Ramaze::Cache::MemCache with all the custom options set. All options set in this method will be sent to Dalli as well.
Using this method allows you to use different memcache settings for various parts of Ramaze. For example, you might want to use servers A and B for storing the sessions but server C for only views. Most of the way this method works was inspired by Ramaze::Cache::Sequel which was contributed by Lars Olsson.
87 88 89 90 |
# File 'lib/ramaze/cache/memcache.rb', line 87 def using( = {}) merged = Ramaze::Cache::MemCache.trait[:default].merge() Class.new(self) { = merged } end |
Instance Method Details
- (Object) cache_clear
Removes all items from the cache.
140 141 142 |
# File 'lib/ramaze/cache/memcache.rb', line 140 def cache_clear @client.flush end |
- (Object) cache_delete(*keys)
Removes the specified keys from the cache.
151 152 153 154 155 |
# File 'lib/ramaze/cache/memcache.rb', line 151 def cache_delete(*keys) keys.each do |key| @client.delete(key) end end |
- (Mixed) cache_fetch(key, default = nil)
Fetches the specified key from the cache. It the value was nil the default value will be returned instead.
167 168 169 170 171 172 173 174 175 |
# File 'lib/ramaze/cache/memcache.rb', line 167 def cache_fetch(key, default = nil) value = @client.get(key) if value.nil? return default else return value end end |
- (Object) cache_setup(hostname, username, appname, cachename)
Prepares the cache by creating the namespace and an instance of a Dalli client.
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ramaze/cache/memcache.rb', line 121 def cache_setup(hostname, username, appname, cachename) # Validate the maximum TTL if [:expires_in] > MAX_TTL raise(ArgumentError, "The maximum TTL of Memcache is 30 days") end [:namespace] = [ hostname, username, appname, cachename ].compact.join('-') @client = ::Dalli::Client.new([:servers], ) end |
- (Mixed) cache_store(key, value, ttl = nil, options = {})
Sets the given key to the specified value. Optionally you can specify a hash with options specific to the key. Once a key has been stored it’s value will be returned.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ramaze/cache/memcache.rb', line 191 def cache_store(key, value, ttl = nil, = {}) ttl = .delete(:ttl) || [:expires_in] if ttl > MAX_TTL raise(ArgumentError, "The maximum TTL of Memcache is 30 days") end @client.set(key, value, ttl, ) return value end |