Class: Parse::Cache::Redis
- Inherits:
-
Object
- Object
- Parse::Cache::Redis
- Defined in:
- lib/parse/cache/redis.rb
Overview
Ergonomic Redis cache builder for Parse Stack. Composes a
ConnectionPool of Moneta-Redis stores and carries an optional
namespace that Parse::Client will pick up automatically — there
is no need to also pass cache_namespace: to Parse.setup when
using this wrapper.
Usage:
Parse.setup(
cache: Parse::Cache::Redis.new(
url: "redis://localhost:6379/0",
namespace: "app_x",
pool_size: 10,
),
expires: 60,
...
)
The instance is a Moneta-compatible store (it delegates the four
methods the Faraday caching middleware uses — [], key?,
delete, store — to a pooled backend), so it can be passed
directly to Parse.setup(cache:) / Parse::Client.new(cache:).
Instance Attribute Summary collapse
-
#namespace ⇒ String?
readonly
Cache key namespace prefix (or nil if not set).
-
#pool_size ⇒ Integer
readonly
Pool size.
-
#url ⇒ String
readonly
Redis connection URL.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#clear(scope: nil) ⇒ Object
Clear cached entries belonging to this wrapper.
-
#close ⇒ Object
Close all pooled connections.
-
#create(key, value, options = {}) ⇒ Object
Atomic SETNX.
- #delete(key) ⇒ Object
-
#flush_db! ⇒ Object
Issue
FLUSHDBon the backing Redis DB, regardless of whether a namespace is configured. -
#increment(key, amount = 1, options = {}) ⇒ Object
Atomic counter increment.
-
#initialize(url:, namespace: nil, pool_size: 5, pool_timeout: 5, **moneta_options) ⇒ Redis
constructor
A new instance of Redis.
- #key?(key) ⇒ Boolean
- #store(key, value, options = {}) ⇒ Object
Constructor Details
#initialize(url:, namespace: nil, pool_size: 5, pool_timeout: 5, **moneta_options) ⇒ Redis
Returns a new instance of Redis.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/parse/cache/redis.rb', line 73 def initialize(url:, namespace: nil, pool_size: 5, pool_timeout: 5, **) @url = url @namespace = normalize_namespace(namespace) @pool_size = pool_size @pool_timeout = pool_timeout # Default expires: true so per-call `expires:` (the TTL the # Faraday caching middleware passes on store) is honored. The # Moneta-Redis adapter ignores per-call expires unless the # store was constructed with this flag. Without it, cached # session-scoped REST responses outlive their token's # validity. Callers can still pass `expires: false` to opt out. = { expires: true }.merge() @moneta_options = @closed = false @pool = Pool.new(size: pool_size, timeout: pool_timeout) do Moneta.new(:Redis, { url: url }.merge()) end end |
Instance Attribute Details
#namespace ⇒ String? (readonly)
Returns cache key namespace prefix (or nil if not set).
32 33 34 |
# File 'lib/parse/cache/redis.rb', line 32 def namespace @namespace end |
#pool_size ⇒ Integer (readonly)
Returns pool size.
35 36 37 |
# File 'lib/parse/cache/redis.rb', line 35 def pool_size @pool_size end |
#url ⇒ String (readonly)
Returns Redis connection URL.
38 39 40 |
# File 'lib/parse/cache/redis.rb', line 38 def url @url end |
Instance Method Details
#[](key) ⇒ Object
92 93 94 |
# File 'lib/parse/cache/redis.rb', line 92 def [](key) @pool[key] end |
#clear(scope: nil) ⇒ Object
Clear cached entries belonging to this wrapper. Required for
Parse::Client#clear_cache! compatibility.
Namespace-scoped when a namespace is set: the wrapper walks
<namespace>:* via Redis SCAN and DELs the matching keys,
leaving other tenants on the same DB untouched. When no
namespace is configured the wrapper falls back to FLUSHDB on
the backing DB — same blast radius as previous versions, but
only for unnamespaced deployments. To opt into the wide
FLUSHDB explicitly (e.g. ops tooling), call #flush_db!.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/parse/cache/redis.rb', line 142 def clear(scope: nil) if scope prefix = validate_scope!(scope) delete_keys_matching!("#{prefix}:*") elsif @namespace delete_keys_matching!("#{@namespace}:*") else @pool.clear end self end |
#close ⇒ Object
Close all pooled connections. Safe to call multiple times.
164 165 166 167 168 |
# File 'lib/parse/cache/redis.rb', line 164 def close return if @closed @closed = true @pool.close end |
#create(key, value, options = {}) ⇒ Object
Atomic SETNX. Required so Parse::CreateLock can acquire
cross-process locks when this wrapper is the configured cache /
synchronize_create_store. Returns true only when the key did
not already exist.
112 113 114 |
# File 'lib/parse/cache/redis.rb', line 112 def create(key, value, = {}) @pool.create(key, value, ) end |
#delete(key) ⇒ Object
100 101 102 |
# File 'lib/parse/cache/redis.rb', line 100 def delete(key) @pool.delete(key) end |
#flush_db! ⇒ Object
Issue FLUSHDB on the backing Redis DB, regardless of whether a
namespace is configured. Evicts every key on the selected DB,
including unrelated tenants — use only for ops tooling that
owns the whole DB.
158 159 160 161 |
# File 'lib/parse/cache/redis.rb', line 158 def flush_db! @pool.clear self end |
#increment(key, amount = 1, options = {}) ⇒ Object
Atomic counter increment. Forwarded for Moneta surface parity.
117 118 119 |
# File 'lib/parse/cache/redis.rb', line 117 def increment(key, amount = 1, = {}) @pool.increment(key, amount, ) end |
#key?(key) ⇒ Boolean
96 97 98 |
# File 'lib/parse/cache/redis.rb', line 96 def key?(key) @pool.key?(key) end |
#store(key, value, options = {}) ⇒ Object
104 105 106 |
# File 'lib/parse/cache/redis.rb', line 104 def store(key, value, = {}) @pool.store(key, value, ) end |