Class: Moneta::Adapters::Redis

Inherits:
Moneta::Adapter show all
Includes:
ExpiresSupport
Defined in:
lib/moneta/adapters/redis.rb

Overview

Redis backend

Instance Attribute Summary

Attributes inherited from Moneta::Adapter

#backend

Instance Method Summary collapse

Methods included from ExpiresSupport

included

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

Methods included from Defaults

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

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Object

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`



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

backend { |**options| ::Redis.new(options) }

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


77
78
79
80
# File 'lib/moneta/adapters/redis.rb', line 77

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

#closeObject

Explicitly close the store

Returns:

  • nil



95
96
97
98
# File 'lib/moneta/adapters/redis.rb', line 95

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/moneta/adapters/redis.rb', line 83

def create(key, value, options = {})
  expires = expires_value(options, config.expires)

  if @backend.setnx(key, value)
    update_expires(@backend, 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



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

def delete(key, options = {})
  future = nil
  @backend.pipelined do |pipeline|
    future = pipeline.get(key)
    pipeline.del(key)
  end
  future.value
end

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

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

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


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

def each_key(&block)
  return enum_for(:each_key) unless block_given?

  @backend.scan_each { |k| yield(k) }
  self
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



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

def increment(key, amount = 1, options = {})
  with_expiry_update(key, **options) do |pipeline_handle|
    pipeline_handle.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)


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

def key?(key, options = {})
  with_expiry_update(key, default: nil, **options) do |pipeline_handle|
    if pipeline_handle.respond_to?(:exists?)
      pipeline_handle.exists?(key)
    else
      pipeline_handle.exists(key)
    end
  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



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

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

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

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

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/moneta/adapters/redis.rb', line 108

def merge!(pairs, options = {})
  keys = pairs.map { |key, _| key }.to_a
  return self if keys.empty?

  if block_given?
    old_values = @backend.mget(*keys)
    updates = pairs.each_with_index.with_object({}) do |(pair, i), updates|
      old_value = old_values[i]
      if old_value != nil
        key, new_value = pair
        updates[key] = yield(key, old_value, new_value)
      end
    end
    unless updates.empty?
      pairs = if pairs.respond_to?(:merge)
                pairs.merge(updates)
              else
                Hash[pairs.to_a].merge!(updates)
              end
    end
  end

  with_expiry_update(*keys, **options) do |pipeline_handle|
    pipeline_handle.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



49
50
51
52
53
54
55
56
57
# File 'lib/moneta/adapters/redis.rb', line 49

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

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

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:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values



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

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