Class: Moneta::Adapters::Redis

Inherits:
Object
  • Object
show all
Includes:
Defaults, ExpiresSupport
Defined in:
lib/moneta/adapters/redis.rb

Overview

Redis backend

Instance Attribute Summary

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #close, #decrement, #fetch

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Default expiration time

  • Other (Object)

    options passed to `Redis#new`


14
15
16
17
# File 'lib/moneta/adapters/redis.rb', line 14

def initialize(options = {})
  self.default_expires = options.delete(:expires)
  @redis = ::Redis.new(options)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

62
63
64
65
# File 'lib/moneta/adapters/redis.rb', line 62

def clear(options = {})
  @redis.flushdb
  self
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

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

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


47
48
49
50
51
52
# File 'lib/moneta/adapters/redis.rb', line 47

def delete(key, options = {})
  if value = load(key, options)
    @redis.del(key)
    value
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value from store


55
56
57
58
59
# File 'lib/moneta/adapters/redis.rb', line 55

def increment(key, amount = 1, options = {})
  value = @redis.incrby(key, amount)
  update_expires(key, options)
  value
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

20
21
22
23
24
25
26
27
# File 'lib/moneta/adapters/redis.rb', line 20

def key?(key, options = {})
  if @redis.exists(key)
    update_expires(key, options, nil)
    true
  else
    false
  end
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn't exist

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


30
31
32
33
34
# File 'lib/moneta/adapters/redis.rb', line 30

def load(key, options = {})
  value = @redis.get(key)
  update_expires(key, options, nil)
  value
end

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

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


37
38
39
40
41
42
43
44
# File 'lib/moneta/adapters/redis.rb', line 37

def store(key, value, options = {})
  if expires = expires_value(options)
    @redis.setex(key, expires, value)
  else
    @redis.set(key, value)
  end
  value
end