Class: Ramaze::Cache::Redis

Inherits:
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 (http://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 .using() on the class:

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

Author:

Since:

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Redis) initialize(options = {})

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



76
77
78
79
80
81
82
# File 'lib/ramaze/cache/redis.rb', line 76

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

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

Class Attribute Details

+ (Object) options

Returns the value of attribute options

Since:

  • 09-10-2011



42
43
44
# File 'lib/ramaze/cache/redis.rb', line 42

def options
  @options
end

Instance Attribute Details

- (Object) options

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

Since:

  • 09-10-2011



39
40
41
# File 'lib/ramaze/cache/redis.rb', line 39

def options
  @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 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



61
62
63
64
# File 'lib/ramaze/cache/redis.rb', line 61

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

Instance Method Details

- (Object) cache_clear

Clears the entire cache.

Author:

  • Michael Fellinger

Since:

  • 09-10-2011



111
112
113
# File 'lib/ramaze/cache/redis.rb', line 111

def cache_clear
  @client.flushall
end

- (Object) cache_delete(*keys)

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



121
122
123
# File 'lib/ramaze/cache/redis.rb', line 121

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

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



134
135
136
137
# File 'lib/ramaze/cache/redis.rb', line 134

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

- (Object) cache_setup(hostname, username, appname, cachename)

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



97
98
99
100
101
102
103
# File 'lib/ramaze/cache/redis.rb', line 97

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

  @client = ::Redis.new(options)
end

- (Object) cache_store(key, value, ttl = nil, options = {})

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) (defaults to: nil)

    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



149
150
151
152
153
154
# File 'lib/ramaze/cache/redis.rb', line 149

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

  return value
end