Class: Ramaze::Cache::Moneta
- Inherits:
-
Object
- Object
- Ramaze::Cache::Moneta
- Includes:
- Cache::API, Innate::Traited
- Defined in:
- lib/ramaze/cache/moneta.rb
Overview
The Moneta cache is a cache driver for Moneta (http://github.com/minad/moneta). Moneta is a unified interface to key/value stores.
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::Moneta
If you want to specify custom options you can do so by calling Moneta.using on the class:
Ramaze::Cache.options.session = Ramaze::Cache::Moneta.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(*args)
Prepares the cache by setting up the prefix and loading Moneta.
-
- (Object) cache_store(key, value, ttl = nil, options = {})
Stores a new value under the given key.
-
- (Moneta) initialize(options = {})
constructor
Creates a new instance of the cache and merges the options if they haven't already been set.
Methods included from Innate::Traited
#ancestral_trait, #ancestral_trait_values, #class_trait, #each_ancestral_trait, included, #trait
Constructor Details
- (Moneta) initialize(options = {})
Creates a new instance of the cache and merges the options if they haven't already been set.
75 76 77 78 79 80 |
# File 'lib/ramaze/cache/moneta.rb', line 75 def initialize( = {}) self.class. ||= Ramaze::Cache::Moneta.trait[:default].merge() @options = .merge(self.class.) end |
Class Attribute Details
+ (Object) options
Returns the value of attribute options
44 45 46 |
# File 'lib/ramaze/cache/moneta.rb', line 44 def @options end |
Instance Attribute Details
- (Object) options
Hash containing all the default options merged with the user specified ones.
41 42 43 |
# File 'lib/ramaze/cache/moneta.rb', line 41 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 Moneta cache for your sessions could be located at server #1 while a custom cache is located on server #2.
60 61 62 63 |
# File 'lib/ramaze/cache/moneta.rb', line 60 def using( = {}) merged = Ramaze::Cache::Moneta.trait[:default].merge() Class.new(self) { @options = merged } end |
Instance Method Details
- (Object) cache_clear
Clears the entire cache.
100 101 102 |
# File 'lib/ramaze/cache/moneta.rb', line 100 def cache_clear @moneta.clear end |
- (Object) cache_delete(*keys)
Removes a number of keys from the cache.
110 111 112 |
# File 'lib/ramaze/cache/moneta.rb', line 110 def cache_delete(*keys) keys.each {|key| @moneta.delete(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.
123 124 125 |
# File 'lib/ramaze/cache/moneta.rb', line 123 def cache_fetch(key, default = nil) @moneta.fetch(key, default) end |
- (Object) cache_setup(*args)
Prepares the cache by setting up the prefix and loading Moneta.
87 88 89 90 91 92 93 |
# File 'lib/ramaze/cache/moneta.rb', line 87 def cache_setup(*args) opts = .dup opts[:prefix] = ['ramaze', *args].compact.join(':') opts[:expires] = opts.delete(:expires_in) adapter = opts.delete(:adapter) @moneta = ::Moneta.new(adapter, ) end |
- (Object) cache_store(key, value, ttl = nil, options = {})
Stores a new value under the given key.
137 138 139 140 |
# File 'lib/ramaze/cache/moneta.rb', line 137 def cache_store(key, value, ttl = nil, = {}) [:expires] = .delete(:ttl) || @options[:expires_in] @moneta.store(key, value, ) end |