Module: CMDx::Stores
- Extended by:
- Stores
- Included in:
- Stores
- Defined in:
- lib/cmdx/stores.rb,
lib/cmdx/stores/redis.rb,
lib/cmdx/stores/memory.rb
Overview
Key/value scratch space for idempotency keys, locks, rate limits, circuit breakers, checkpoints, and cached results. Stores delegates to Configuration#store; swap the configured backend (for example Memory or Redis) at boot.
Defined Under Namespace
Instance Method Summary collapse
-
#decr(key, ttl: nil) ⇒ Integer
Value after decrement.
-
#del(key) ⇒ Integer
1when a key was removed,0otherwise. -
#del_if(key, expected) ⇒ Boolean
Whether the key was removed.
-
#get(key) ⇒ String?
Stored value, or
nilwhen missing or expired. -
#incr(key, ttl: nil) ⇒ Integer
Value after increment.
- #key!(key) ⇒ String private
- #nx_xx!(nx, xx) ⇒ void private
-
#remaining_ttl(key) ⇒ Integer?
Seconds until expiry when the backend supports #ttl;
nilwhen the key is missing, expired, or TTL is unavailable. -
#set(key, value, ttl: nil, nx: false, xx: false) ⇒ Boolean
Whether the value was written.
- #ttl!(ttl) ⇒ Integer private
Instance Method Details
#decr(key, ttl: nil) ⇒ Integer
Returns value after decrement.
43 44 45 |
# File 'lib/cmdx/stores.rb', line 43 def decr(key, ttl: nil) CMDx.configuration.store.decr(key, ttl:) end |
#del(key) ⇒ Integer
Returns 1 when a key was removed, 0 otherwise.
50 51 52 |
# File 'lib/cmdx/stores.rb', line 50 def del(key) CMDx.configuration.store.del(key) end |
#del_if(key, expected) ⇒ Boolean
Returns whether the key was removed.
59 60 61 62 63 64 |
# File 'lib/cmdx/stores.rb', line 59 def del_if(key, expected) store = CMDx.configuration.store raise NotImplementedError, "store must implement del_if(key, expected) for lock release" unless store.respond_to?(:del_if) store.del_if(key, expected) end |
#get(key) ⇒ String?
Returns stored value, or nil when missing or expired.
16 17 18 |
# File 'lib/cmdx/stores.rb', line 16 def get(key) CMDx.configuration.store.get(key) end |
#incr(key, ttl: nil) ⇒ Integer
Returns value after increment.
35 36 37 |
# File 'lib/cmdx/stores.rb', line 35 def incr(key, ttl: nil) CMDx.configuration.store.incr(key, ttl:) end |
#key!(key) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
82 83 84 85 86 87 |
# File 'lib/cmdx/stores.rb', line 82 def key!(key) str = key.to_s raise ArgumentError, "store key must be non-empty" if str.empty? str end |
#nx_xx!(nx, xx) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
104 105 106 107 108 |
# File 'lib/cmdx/stores.rb', line 104 def nx_xx!(nx, xx) return unless nx && xx raise ArgumentError, "store set cannot use nx and xx together" end |
#remaining_ttl(key) ⇒ Integer?
Returns seconds until expiry when the backend supports
#ttl; nil when the key is missing, expired, or TTL is unavailable.
70 71 72 73 74 75 76 |
# File 'lib/cmdx/stores.rb', line 70 def remaining_ttl(key) store = CMDx.configuration.store return unless store.respond_to?(:ttl) seconds = store.ttl(key) seconds if seconds.is_a?(Integer) && seconds.positive? end |
#set(key, value, ttl: nil, nx: false, xx: false) ⇒ Boolean
Returns whether the value was written.
27 28 29 |
# File 'lib/cmdx/stores.rb', line 27 def set(key, value, ttl: nil, nx: false, xx: false) CMDx.configuration.store.set(key, value, ttl:, nx:, xx:) end |
#ttl!(ttl) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 96 97 |
# File 'lib/cmdx/stores.rb', line 93 def ttl!(ttl) return ttl if ttl.is_a?(Integer) && ttl.positive? raise ArgumentError, "store ttl must be a positive Integer (got #{ttl.inspect})" end |