Class: Moneta::Adapters::Redis
- Inherits:
-
Object
- Object
- Moneta::Adapters::Redis
- Includes:
- Defaults, ExpiresSupport
- Defined in:
- lib/moneta/adapters/redis.rb
Overview
Redis backend
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#each_key(&block) ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ Redis
constructor
A new instance of Redis.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from ExpiresSupport
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 = {}) ⇒ Redis
Returns a new instance of Redis.
18 19 20 21 |
# File 'lib/moneta/adapters/redis.rb', line 18 def initialize( = {}) self.default_expires = .delete(:expires) @backend = [:backend] || ::Redis.new() end |
Instance Attribute Details
#backend ⇒ Object (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
81 82 83 84 |
# File 'lib/moneta/adapters/redis.rb', line 81 def clear( = {}) @backend.flushdb self end |
#close ⇒ Object
Explicitly close the store
99 100 101 102 |
# File 'lib/moneta/adapters/redis.rb', line 99 def close @backend.quit nil end |
#create(key, value, options = {}) ⇒ Boolean
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.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/moneta/adapters/redis.rb', line 87 def create(key, value, = {}) expires = expires_value(, @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
64 65 66 67 68 69 70 71 |
# File 'lib/moneta/adapters/redis.rb', line 64 def delete(key, = {}) future = nil @backend.pipelined do future = @backend.get(key) @backend.del(key) end future.value end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
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.
38 39 40 41 42 43 |
# File 'lib/moneta/adapters/redis.rb', line 38 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
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.
74 75 76 77 78 |
# File 'lib/moneta/adapters/redis.rb', line 74 def increment(key, amount = 1, = {}) with_expiry_update(key, **) 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.
27 28 29 30 31 32 33 34 35 |
# File 'lib/moneta/adapters/redis.rb', line 27 def key?(key, = {}) with_expiry_update(key, default: nil, **) do if @backend.respond_to?(:exists?) @backend.exists?(key) else @backend.exists(key) end end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
46 47 48 49 50 |
# File 'lib/moneta/adapters/redis.rb', line 46 def load(key, = {}) with_expiry_update(key, default: nil, **) do @backend.get(key) end end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
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.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/moneta/adapters/redis.rb', line 112 def merge!(pairs, = {}) keys = pairs.map { |key, _| key } 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, **) do @backend.mset(*pairs.to_a.flatten(1)) end self end |
#store(key, value, options = {}) ⇒ Object
Store value with key
53 54 55 56 57 58 59 60 61 |
# File 'lib/moneta/adapters/redis.rb', line 53 def store(key, value, = {}) if expires = expires_value() 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>
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.
105 106 107 108 109 |
# File 'lib/moneta/adapters/redis.rb', line 105 def values_at(*keys, **) with_expiry_update(*keys, default: nil, **) do @backend.mget(*keys) end end |