Class: CMDx::Stores::Redis
- Inherits:
-
Object
- Object
- CMDx::Stores::Redis
- Defined in:
- lib/cmdx/stores/redis.rb
Overview
Delegates to a Redis-compatible client (redis-rb, MockRedis, etc.).
Optional namespace prefixes keys when Redis is shared with other data.
The client must respond to get, set, incr, expire, ttl, del, and eval.
Constant Summary collapse
- DEL_IF_SCRIPT =
<<~LUA if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end LUA
Instance Method Summary collapse
- #decr(key, ttl: nil) ⇒ Integer
- #del(key) ⇒ Integer
-
#del_if(key, expected) ⇒ Boolean
Whether the key was removed.
- #get(key) ⇒ String?
- #incr(key, ttl: nil) ⇒ Integer
-
#initialize(redis:, namespace: EMPTY_STRING) ⇒ Redis
constructor
A new instance of Redis.
- #set(key, value, ttl: nil, nx: false, xx: false) ⇒ Boolean
-
#ttl(key) ⇒ Integer?
Seconds until expiry, or
nilwhen missing or expired.
Constructor Details
#initialize(redis:, namespace: EMPTY_STRING) ⇒ Redis
Returns a new instance of Redis.
22 23 24 25 |
# File 'lib/cmdx/stores/redis.rb', line 22 def initialize(redis:, namespace: EMPTY_STRING) @redis = redis @namespace = namespace_prefix(namespace) end |
Instance Method Details
#decr(key, ttl: nil) ⇒ Integer
81 82 83 84 85 86 |
# File 'lib/cmdx/stores/redis.rb', line 81 def decr(key, ttl: nil) key = prefixed(key) Stores.ttl!(ttl) if ttl @redis.decr(key) end |
#del(key) ⇒ Integer
90 91 92 |
# File 'lib/cmdx/stores/redis.rb', line 90 def del(key) @redis.del(prefixed(key)) end |
#del_if(key, expected) ⇒ Boolean
Returns whether the key was removed.
97 98 99 100 101 |
# File 'lib/cmdx/stores/redis.rb', line 97 def del_if(key, expected) # rubocop:disable Naming/PredicateMethod -- store delete contract key = prefixed(key) deleted = @redis.eval(DEL_IF_SCRIPT, keys: [key], argv: [expected.to_s]) deleted.to_i.positive? end |
#get(key) ⇒ String?
29 30 31 |
# File 'lib/cmdx/stores/redis.rb', line 29 def get(key) @redis.get(prefixed(key)) end |
#incr(key, ttl: nil) ⇒ Integer
62 63 64 65 66 67 68 69 |
# File 'lib/cmdx/stores/redis.rb', line 62 def incr(key, ttl: nil) key = prefixed(key) ttl = Stores.ttl!(ttl) if ttl count = @redis.incr(key) @redis.expire(key, ttl) if ttl count end |
#set(key, value, ttl: nil, nx: false, xx: false) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cmdx/stores/redis.rb', line 40 def set(key, value, ttl: nil, nx: false, xx: false) # rubocop:disable Naming/PredicateMethod -- Redis SET contract key = prefixed(key) ttl = Stores.ttl!(ttl) if ttl Stores.nx_xx!(nx, xx) = {} [:ex] = ttl if ttl [:nx] = true if nx [:xx] = true if xx if .empty? @redis.set(key, value.to_s) true else !!@redis.set(key, value.to_s, **) end end |
#ttl(key) ⇒ Integer?
Returns seconds until expiry, or nil when missing or expired.
73 74 75 76 77 |
# File 'lib/cmdx/stores/redis.rb', line 73 def ttl(key) key = prefixed(key) seconds = @redis.ttl(key) seconds if seconds.is_a?(Integer) && seconds.positive? end |