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.
-
#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.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #decrement, #features, #fetch, included, #supports?
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
69 70 71 72 |
# File 'lib/moneta/adapters/redis.rb', line 69 def clear( = {}) @backend.flushdb self end |
#close ⇒ Object
Explicitly close the store
85 86 87 88 |
# File 'lib/moneta/adapters/redis.rb', line 85 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.
75 76 77 78 79 80 81 82 |
# File 'lib/moneta/adapters/redis.rb', line 75 def create(key, value, = {}) if @backend.setnx(key, value) update_expires(key, ) true else false end end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
54 55 56 57 58 59 |
# File 'lib/moneta/adapters/redis.rb', line 54 def delete(key, = {}) if value = load(key, ) @backend.del(key) value end 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.
62 63 64 65 66 |
# File 'lib/moneta/adapters/redis.rb', line 62 def increment(key, amount = 1, = {}) value = @backend.incrby(key, amount) update_expires(key, ) value 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 |
# File 'lib/moneta/adapters/redis.rb', line 27 def key?(key, = {}) if @backend.exists(key) update_expires(key, , nil) true else false end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
37 38 39 40 41 |
# File 'lib/moneta/adapters/redis.rb', line 37 def load(key, = {}) value = @backend.get(key) update_expires(key, , nil) value end |
#store(key, value, options = {}) ⇒ Object
Store value with key
44 45 46 47 48 49 50 51 |
# File 'lib/moneta/adapters/redis.rb', line 44 def store(key, value, = {}) if expires = expires_value() @backend.setex(key, expires, value) else @backend.set(key, value) end value end |