Class: Moneta::Adapters::Redis
- Inherits:
-
Object
- Object
- Moneta::Adapters::Redis
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/redis.rb
Overview
Redis backend
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#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
#[], #[]=, #close, #decrement, #fetch
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Redis
Returns a new instance of Redis.
13 14 15 16 |
# File 'lib/moneta/adapters/redis.rb', line 13 def initialize( = {}) @expires = .delete(:expires) @redis = ::Redis.new() end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
66 67 68 69 |
# File 'lib/moneta/adapters/redis.rb', line 66 def clear( = {}) @redis.flushdb self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
50 51 52 53 54 55 |
# File 'lib/moneta/adapters/redis.rb', line 50 def delete(key, = {}) if value = load(key, ) @redis.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.
58 59 60 61 62 63 |
# File 'lib/moneta/adapters/redis.rb', line 58 def increment(key, amount = 1, = {}) value = @redis.incrby(key, amount) expires = ([:expires] || @expires) @redis.expire(key, expires) if expires value end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/moneta/adapters/redis.rb', line 19 def key?(key, = {}) if @redis.exists(key) if expires = [:expires] @redis.expire(key, expires) end true else false end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
31 32 33 34 35 36 37 |
# File 'lib/moneta/adapters/redis.rb', line 31 def load(key, = {}) value = @redis.get(key) if value && (expires = [:expires]) @redis.expire(key, expires) end value end |
#store(key, value, options = {}) ⇒ Object
Store value with key
40 41 42 43 44 45 46 47 |
# File 'lib/moneta/adapters/redis.rb', line 40 def store(key, value, = {}) if expires = ([:expires] || @expires) @redis.setex(key, expires, value) else @redis.set(key, value) end value end |