Class: Ramaze::Cache::MemCache

Inherits:
Object
  • Object
show all
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 information about Memcache can be found on it’s website: <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 using the using() method:

Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using(
  :compression => false
)

All options sent to the using() method will be sent to Dalli.

Examples:

Using the default options

Ramaze::Cache.options.view = Ramaze::Cache::MemCache
Ramaze.setup_dependencies

Ramaze::Cache.view.store(:my_view, 'Hello Ramaze')

Using custom options

Ramaze::Cache.options.view = Ramaze::Cache::MemCache.using(
  :compression => false,
  :servers     => ['localhost:11211', 'localhost:112112']
)

Ramaze.setup_dependencies
Ramaze::Cache.view.store(:my_view, 'Hello Ramaze')

Author:

  • Yorick Peterse

Since:

  • 04-05-2011

Constant Summary collapse

MAX_TTL =

The maximum Time To Live that can be used in Memcache

Since:

  • 04-05-2011

2592000

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MemCache

Creates a new instance of the cache class.

Parameters:

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

    A hash with custom options, see Ramaze::Cache::MemCache.using for all available options.

Author:

  • Michael Fellinger

Since:

  • 04-05-2011



116
117
118
119
120
121
122
# File 'lib/ramaze/cache/memcache.rb', line 116

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

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

Class Attribute Details

.optionsObject

Since:

  • 04-05-2011



74
75
76
# File 'lib/ramaze/cache/memcache.rb', line 74

def options
  @options
end

Instance Attribute Details

#optionsObject

Hash containing all the default options merged with the user specified ones

Since:

  • 04-05-2011



71
72
73
# File 'lib/ramaze/cache/memcache.rb', line 71

def options
  @options
end

Class Method Details

.using(options = {}) ⇒ Object

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.

Examples:

Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using(
  :compression => false,
  :username    => 'ramaze',
  :password    => 'ramaze123',
  :servers     => ['othermachine.com:12345']
)

Parameters:

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

    A hash containing all configuration options to use for Dalli. For more information on all the available options you can read the README in their repository. This repository can be found here: github.com/mperham/dalli

Author:

  • Yorick Peterse

Since:

  • 04-05-2011



102
103
104
105
# File 'lib/ramaze/cache/memcache.rb', line 102

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

Instance Method Details

#cache_clearObject

Removes all items from the cache.

Author:

  • Yorick Peterse

Since:

  • 04-05-2011



155
156
157
# File 'lib/ramaze/cache/memcache.rb', line 155

def cache_clear
  @client.flush
end

#cache_delete(*keys) ⇒ Object

Removes the specified keys from the cache.

Parameters:

  • keys (Array)

    The keys to remove from the cache.

Author:

  • Yorick Peterse

Since:

  • 04-05-2011



166
167
168
169
170
# File 'lib/ramaze/cache/memcache.rb', line 166

def cache_delete(*keys)
  keys.each do |key|
    @client.delete(key)
  end
end

#cache_fetch(key, default = nil) ⇒ Mixed

Fetches the specified key from the cache. It the value was nil the default value 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:

  • Yorick Peterse

Since:

  • 04-05-2011



182
183
184
185
186
187
188
189
190
# File 'lib/ramaze/cache/memcache.rb', line 182

def cache_fetch(key, default = nil)
  value = @client.get(key)

  if value.nil?
    return default
  else
    return value
  end
end

#cache_setup(hostname, username, appname, cachename) ⇒ Object

Prepares the cache by creating the namespace and an instance of a Dalli client.

Parameters:

  • hostname (String)

    The hostname of the machine running the application.

  • username (String)

    The name of the user executing the process

  • appname (String)

    Unique identifier for the application.

  • cachename (String)

    The namespace to use for this cache instance.

Author:

  • Yorick Peterse

Since:

  • 04-05-2011



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ramaze/cache/memcache.rb', line 136

def cache_setup(hostname, username, appname, cachename)
  # Validate the maximum TTL
  if options[:expires_in] > MAX_TTL
    raise(ArgumentError, "The maximum TTL of Memcache is 30 days")
  end

  options[:namespace] = [
    hostname, username, appname, cachename
  ].compact.join('-')

  @client = ::Dalli::Client.new(options[:servers], options)
end

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

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.

Parameters:

  • key (String)

    The name of the key to store.

  • value (Mixed)

    The value to store in Memcache.

  • ttl (Fixnum)

    The Time To Live to use for the current key.

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

    A hash containing options specific for the specified key.

Returns:

  • (Mixed)

Author:

  • Yorick Peterse

Since:

  • 04-05-2011



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ramaze/cache/memcache.rb', line 206

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

  if ttl > MAX_TTL
    raise(ArgumentError, "The maximum TTL of Memcache is 30 days")
  end

  @client.set(key, value, ttl, options)

  return value
end