Files

Padrino::Cache::Store::Redis

Redis Cache Store

Public Class Methods

new(client, options={}) click to toggle source

Initialize Redis store with client connection.

@param client

Instance of Redis client

@example

Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
# or from your app
set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
# you can provide a marshal parser (to store ruby objects)
set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0), :parser => :marshal)

@api public

# File lib/padrino-cache/store/redis.rb, line 22
def initialize(client, options={})
  @backend = client
  super(options)
end

Public Instance Methods

delete(key) click to toggle source

Delete the value for a given key

@param [String] key

cache key

@example

# with: MyApp.cache.set('records', records)
MyApp.cache.delete('records')

@api public

# File lib/padrino-cache/store/redis.rb, line 79
def delete(key)
  @backend.del(key)
end
flush() click to toggle source

Reinitialize your cache

@example

# with: MyApp.cache.set('records', records)
MyApp.cache.flush
MyApp.cache.get('records') # => nil

@api public

# File lib/padrino-cache/store/redis.rb, line 92
def flush
  @backend.flushdb
end
get(key) click to toggle source

Return the a value for the given key

@param [String] key

cache key

@example

# with MyApp.cache.set('records', records)
MyApp.cache.get('records')

@api public

# File lib/padrino-cache/store/redis.rb, line 38
def get(key)
  code = @backend.get(key)
  return nil unless code
  parser.decode(code)
end
method_missing(name, *args, &block) click to toggle source

Redis has a ton of powerful features (see: github.com/redis/redis-rb), which we can't use due to how strict the cache library is. This method catches all method calls and tries to pass them on the the redis gem.

@api private

# File lib/padrino-cache/store/redis.rb, line 102
def method_missing(name, *args, &block)
  if @backend.respond_to?(name)
    @backend.send(name, *args, &block)
  else
    super
  end
end
set(key, value, opts = nil) click to toggle source

Set the value for a given key and optionally with an expire time Default expiry is 86400.

@param [String] key

cache key

@param value

value of cache key

@example

MyApp.cache.set('records', records)
MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds

@api public

# File lib/padrino-cache/store/redis.rb, line 58
def set(key, value, opts = nil)
  value = parser.encode(value)
  if opts && opts[:expires_in] && opts[:expires_in] >= 0
    @backend.set(key, value)
    @backend.expireat(key, get_expiry(opts))
  else
    @backend.set(key, value)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.