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 collapse

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #slice, #supports?

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

  • :backend (::Redis)

    Use existing backend instance

  • Other (Object)

    options passed to `Redis#new`


18
19
20
21
# File 'lib/moneta/adapters/redis.rb', line 18

def initialize(options = {})
  self.default_expires = options.delete(:expires)
  @backend = options[:backend] || ::Redis.new(options)
end

Instance Attribute Details

#backendObject (readonly)


12
13
14
# File 'lib/moneta/adapters/redis.rb', line 12

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

69
70
71
72
# File 'lib/moneta/adapters/redis.rb', line 69

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

#closeObject

Explicitly close the store

Returns:

  • nil


87
88
89
90
# File 'lib/moneta/adapters/redis.rb', line 87

def close
  @backend.quit
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

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

Atomically sets a key to value if it's not set.

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set


75
76
77
78
79
80
81
82
83
84
# File 'lib/moneta/adapters/redis.rb', line 75

def create(key, value, options = {})
  expires = expires_value(options, @default_expires)

  if @backend.setnx(key, value)
    update_expires(key, expires)
    true
  else
    false
  end
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 Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


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

def delete(key, options = {})
  future = nil
  @backend.pipelined do
    future = @backend.get(key)
    @backend.del(key)
  end
  future.value
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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store


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

def increment(key, amount = 1, options = {})
  with_expiry_update(key, **options) do
    @backend.incrby(key, amount)
  end
end

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

Exists the value with key

This method considers false and 0 as “no-expire” and every positive number as a time to live in seconds.

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

27
28
29
30
31
# File 'lib/moneta/adapters/redis.rb', line 27

def key?(key, options = {})
  with_expiry_update(key, default: nil, **options) do
    @backend.exists(key)
  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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


34
35
36
37
38
# File 'lib/moneta/adapters/redis.rb', line 34

def load(key, options = {})
  with_expiry_update(key, default: nil, **options) do
    @backend.get(key)
  end
end

#merge!(pairs, options = {}) ⇒ Object


98
99
100
101
102
103
104
# File 'lib/moneta/adapters/redis.rb', line 98

def merge!(pairs, options = {})
  keys = pairs.map { |key, _| key }
  with_expiry_update(*keys, **options) do
    @backend.mset(*pairs.to_a.flatten(1))
  end
  self
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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


41
42
43
44
45
46
47
48
49
# File 'lib/moneta/adapters/redis.rb', line 41

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

#values_at(*keys, **options) ⇒ Object


92
93
94
95
96
# File 'lib/moneta/adapters/redis.rb', line 92

def values_at(*keys, **options)
  with_expiry_update(*keys, default: nil, **options) do
    @backend.mget *keys
  end
end