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 (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
-
.options ⇒ Object
Returns the value of attribute options.
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(*args) ⇒ Object
Prepares the cache by setting up the prefix and loading Moneta.
-
#cache_store(key, value, options = {}) ⇒ Object
Stores a new value under the given key.
-
#initialize(options = {}) ⇒ Moneta
constructor
Creates a new instance of the cache and merges the options if they haven’t already been set.
Constructor Details
#initialize(options = {}) ⇒ Moneta
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
.options ⇒ Object
Returns the value of attribute options.
44 45 46 |
# File 'lib/ramaze/cache/moneta.rb', line 44 def @options end |
Instance Attribute Details
#options ⇒ Object
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
.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 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
#cache_clear ⇒ Object
Clears the entire cache.
100 101 102 |
# File 'lib/ramaze/cache/moneta.rb', line 100 def cache_clear @moneta.clear end |
#cache_delete(*keys) ⇒ Object
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 |
#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.
123 124 125 |
# File 'lib/ramaze/cache/moneta.rb', line 123 def cache_fetch(key, default = nil) @moneta.fetch(key, default) end |
#cache_setup(*args) ⇒ Object
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 |
#cache_store(key, value, options = {}) ⇒ Object
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, = {}) [:expires] = .delete(:ttl) || @options[:expires_in] @moneta.store(key, value, ) end |