Class: Ramaze::Cache::Moneta

Inherits:
Object
  • Object
show all
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.options.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(...)

Examples:

Configuring the Moneta backend

Ramaze::Cache.options.names.push(:moneta)
Ramaze::Cache.options.moneta = Ramaze::Cache::Moneta.using(
  :adapter => :File,
  :dir => './ramaze-cache'
)

Author:

  • Daniel Mendler

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Moneta

Creates a new instance of the cache and merges the options if they haven’t already been set.

Parameters:

  • options (Hash) (defaults to: {})

    A hash with custom options. See Ramaze::Cache::Moneta.using() and the trait :default for more information.

Author:

  • Daniel Mendler



75
76
77
78
79
80
# File 'lib/ramaze/cache/moneta.rb', line 75

def initialize(options = {})
  self.class.options ||=
    Ramaze::Cache::Moneta.trait[:default].merge(options)

  @options = options.merge(self.class.options)
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



44
45
46
# File 'lib/ramaze/cache/moneta.rb', line 44

def options
  @options
end

Instance Attribute Details

#optionsObject

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
  @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.

Parameters:

  • options (Hash) (defaults to: {})

    A hash containing custom options.

Options Hash (options):

  • :expires_in (Fixnum)

    The default time after which a key should expire.

  • :adapter (Symbol)

    Moneta adapter

Author:

  • Daniel Mendler



60
61
62
63
# File 'lib/ramaze/cache/moneta.rb', line 60

def using(options = {})
  merged = Ramaze::Cache::Moneta.trait[:default].merge(options)
  Class.new(self) { @options = merged }
end

Instance Method Details

#cache_clearObject

Clears the entire cache.

Author:

  • Daniel Mendler



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.

Parameters:

  • keys (Array)

    An array of key names to remove.

Author:

  • Daniel Mendler



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.

Parameters:

  • key (String)

    The name of the key to retrieve.

  • default (Mixed) (defaults to: nil)

    The default value.

Returns:

  • (Mixed)

Author:

  • Daniel Mendler



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.

Author:

  • Daniel Mendler



87
88
89
90
91
92
93
# File 'lib/ramaze/cache/moneta.rb', line 87

def cache_setup(*args)
  opts = options.dup
  opts[:prefix] = ['ramaze', *args].compact.join(':')
  opts[:expires] = opts.delete(:expires_in)
  adapter = opts.delete(:adapter)
  @moneta = ::Moneta.new(adapter, options)
end

#cache_store(key, value, options = {}) ⇒ Object

Stores a new value under the given key.

Parameters:

  • key (String)

    The name of the key to store.

  • value (Mixed)

    The value of the key.

  • ttl (Fixnum)

    The Time To Live of the key.

  • options (Hash) (defaults to: {})

    A hash containing key specific options.

Options Hash (options):

  • :expires_in (Object)

    The time after which the key should expire.

Author:

  • Daniel Mendler



137
138
139
140
# File 'lib/ramaze/cache/moneta.rb', line 137

def cache_store(key, value, options = {})
  options[:expires] = options.delete(:ttl) || @options[:expires_in]
  @moneta.store(key, value, options)
end