Class: McpAuthorization::Cache::RedisStore
- Inherits:
-
Object
- Object
- McpAuthorization::Cache::RedisStore
- Defined in:
- lib/mcp_authorization/cache/redis_store.rb
Overview
Redis-backed store — shared across workers and hosts. Values are stored as JSON (the tools/list result is plain JSON-serializable data), with a per-entry TTL via SET … EX.
Connection resolution (first match wins), so a Rails host gets the “Rails redis config” for free without passing anything:
1. an explicit client — RedisStore.new(redis: $redis)
2. an explicit URL — RedisStore.new(url: "redis://…")
3. ENV["REDIS_URL"] — the conventional Rails/Heroku/Sidekiq var
4. Redis.new — the redis gem's own default (ENV or localhost),
matching a bare `Redis.new` in the host
The redis gem is an optional dependency, required lazily here so hosts that don’t use this store never need it.
Defined Under Namespace
Classes: RedisUnavailable
Constant Summary collapse
- NAMESPACE =
"mcpauth".freeze
Instance Method Summary collapse
-
#clear ⇒ Object
: () -> void.
-
#get(key) ⇒ Object
: (String) -> untyped.
-
#initialize(redis: nil, url: nil, namespace: NAMESPACE) ⇒ RedisStore
constructor
: (?redis: untyped?, ?url: String?, ?namespace: String) -> void.
-
#set(key, value, ttl: nil) ⇒ Object
: (String, untyped, ?ttl: Integer?) -> void.
Constructor Details
#initialize(redis: nil, url: nil, namespace: NAMESPACE) ⇒ RedisStore
: (?redis: untyped?, ?url: String?, ?namespace: String) -> void
27 28 29 30 31 |
# File 'lib/mcp_authorization/cache/redis_store.rb', line 27 def initialize(redis: nil, url: nil, namespace: NAMESPACE) @redis = redis @url = url @namespace = namespace end |
Instance Method Details
#clear ⇒ Object
: () -> void
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mcp_authorization/cache/redis_store.rb', line 57 def clear cursor = "0" loop do cursor, keys = client.scan(cursor, match: namespaced("*"), count: 500) client.del(*keys) unless keys.empty? break if cursor == "0" end rescue StandardError => e log_error("clear", e) end |
#get(key) ⇒ Object
: (String) -> untyped
34 35 36 37 38 39 40 |
# File 'lib/mcp_authorization/cache/redis_store.rb', line 34 def get(key) raw = client.get(namespaced(key)) raw && JSON.parse(raw) rescue StandardError => e log_error("get", e) nil # never let a cache outage break tools/list end |
#set(key, value, ttl: nil) ⇒ Object
: (String, untyped, ?ttl: Integer?) -> void
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mcp_authorization/cache/redis_store.rb', line 43 def set(key, value, ttl: nil) payload = JSON.generate(value) if ttl && ttl > 0 client.set(namespaced(key), payload, ex: ttl) else client.set(namespaced(key), payload) end nil rescue StandardError => e log_error("set", e) nil end |