Class: Ramaze::Cache::Redis

Inherits:
Object
  • Object
show all
Includes:
Cache::API, Innate::Traited
Defined in:
lib/ramaze/cache/redis.rb

Overview

The Redis cache is a cache driver for Redis (redis.io/). Redis is a key/value store similar to Memcached but with the ability to flush data to a file among various other features.

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::Redis

If you want to specify custom options you can do so by calling Redis.using on the class:

Ramaze::Cache.options.session = Ramaze::Cache::Redis.using(...)

Examples:

Using a custom Redis host

Ramaze::Cache.options.names.push(:redis)
Ramaze::Cache.options.redis = Ramaze::Cache::Redis.using(
  :host => '123.124.125.126',
  :port => 6478
)

Author:

  • Michael Fellinger

Since:

  • 09-10-2011

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

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::Redis.using() and the trait :default for more information.

Author:

  • Michael Fellinger

Since:

  • 09-10-2011



81
82
83
84
85
86
# File 'lib/ramaze/cache/redis.rb', line 81

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

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

Class Attribute Details

.optionsObject

Since:

  • 09-10-2011



47
48
49
# File 'lib/ramaze/cache/redis.rb', line 47

def options
  @options
end

Instance Attribute Details

#optionsObject

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

Since:

  • 09-10-2011



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

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

  • :host (String)

    The hostname of the machine on which Redis is running.

  • :port (Fixnum)

    The port number to connect to.

Author:

  • Yorick Peterse

Since:

  • 09-10-2011



66
67
68
69
# File 'lib/ramaze/cache/redis.rb', line 66

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

Instance Method Details

#cache_clearObject

Clears the entire cache.

Author:

  • Michael Fellinger

Since:

  • 09-10-2011



115
116
117
# File 'lib/ramaze/cache/redis.rb', line 115

def cache_clear
  @client.flushall
end

#cache_delete(*keys) ⇒ Object

Removes a number of keys from the cache.

Parameters:

  • keys (Array)

    An array of key names to remove.

Author:

  • Michael Fellinger

Since:

  • 09-10-2011



125
126
127
# File 'lib/ramaze/cache/redis.rb', line 125

def cache_delete(*keys)
  @client.del(*keys)
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:

  • Michael Fellinger

Since:

  • 09-10-2011



138
139
140
141
# File 'lib/ramaze/cache/redis.rb', line 138

def cache_fetch(key, default = nil)
  value = @client.get(key)
  value.nil? ? default : ::Marshal.load(value)
end

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

Prepares the cache by setting up the namespace and loading Redis.

Parameters:

  • hostname (String)

    The host of the machine that’s running the Ramaze application.

  • username (String)

    The name of the user that’s running the application.

  • appname (String)

    The name of the application (:pristine by default).

  • cachename (String)

    The namespace to use for this cache instance.

Author:

  • Michael Fellinger

Since:

  • 09-10-2011



101
102
103
104
105
106
107
# File 'lib/ramaze/cache/redis.rb', line 101

def cache_setup(hostname, username, appname, cachename)
  options[:namespace] = [
    'ramaze', hostname, username, appname, cachename
  ].compact.join(':')
  redis_connection = ::Redis.new
  @client = ::Redis::Namespace.new(options[:namespace], redis: redis_connection)
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:

  • Michael Fellinger

Since:

  • 09-10-2011



153
154
155
156
157
158
159
# File 'lib/ramaze/cache/redis.rb', line 153

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

  @client.setex(key, ttl, ::Marshal.dump(value))

  return value
end