Class: Woods::Cache::RedisCacheStore
- Inherits:
-
CacheStore
- Object
- CacheStore
- Woods::Cache::RedisCacheStore
- Defined in:
- lib/woods/cache/redis_cache_store.rb
Overview
Redis-backed cache store using GET/SET with TTL.
Uses simple key-value operations (not Lists like SessionTracer::RedisStore). Values are JSON-serialized on write and deserialized on read. TTL is enforced natively by Redis via the EX option on SET.
Requires the ‘redis` gem at runtime.
Instance Method Summary collapse
-
#clear(namespace: nil) ⇒ void
Clear cached entries by namespace or all woods cache keys.
-
#delete(key) ⇒ void
Delete a key from Redis.
-
#exist?(key) ⇒ Boolean
Check if a key exists in Redis.
-
#initialize(redis:, default_ttl: nil) ⇒ RedisCacheStore
constructor
A new instance of RedisCacheStore.
-
#read(key) ⇒ Object?
Read a value from Redis.
-
#write(key, value, ttl: nil) ⇒ void
Write a value to Redis with optional TTL.
Methods inherited from CacheStore
Constructor Details
#initialize(redis:, default_ttl: nil) ⇒ RedisCacheStore
Returns a new instance of RedisCacheStore.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/woods/cache/redis_cache_store.rb', line 25 def initialize(redis:, default_ttl: nil) super() unless defined?(::Redis) raise ConfigurationError, 'The redis gem is required for RedisCacheStore. Add `gem "redis"` to your Gemfile.' end @redis = redis @default_ttl = default_ttl end |
Instance Method Details
#clear(namespace: nil) ⇒ void
This method returns an undefined value.
Clear cached entries by namespace or all woods cache keys.
Uses SCAN (not KEYS) to avoid blocking Redis on large keyspaces.
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/woods/cache/redis_cache_store.rb', line 101 def clear(namespace: nil) pattern = clear_pattern(namespace) cursor = '0' loop do cursor, keys = @redis.scan(cursor, match: pattern, count: 100) @redis.del(*keys) if keys.any? break if cursor == '0' end rescue ::Redis::BaseError, Errno::ECONNREFUSED, Errno::ECONNRESET => e logger.warn("[Woods] RedisCacheStore#clear failed: #{e.}") nil end |
#delete(key) ⇒ void
This method returns an undefined value.
Delete a key from Redis.
77 78 79 80 81 82 |
# File 'lib/woods/cache/redis_cache_store.rb', line 77 def delete(key) @redis.del(key) rescue ::Redis::BaseError, Errno::ECONNREFUSED, Errno::ECONNRESET => e logger.warn("[Woods] RedisCacheStore#delete failed for #{key}: #{e.}") nil end |
#exist?(key) ⇒ Boolean
Check if a key exists in Redis.
88 89 90 91 92 93 |
# File 'lib/woods/cache/redis_cache_store.rb', line 88 def exist?(key) @redis.exists?(key) rescue ::Redis::BaseError, Errno::ECONNREFUSED, Errno::ECONNRESET => e logger.warn("[Woods] RedisCacheStore#exist? failed for #{key}: #{e.}") false end |
#read(key) ⇒ Object?
Read a value from Redis.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/woods/cache/redis_cache_store.rb', line 40 def read(key) raw = @redis.get(key) return nil unless raw JSON.parse(raw) rescue JSON::ParserError delete_silently(key) nil rescue ::Redis::BaseError, Errno::ECONNREFUSED, Errno::ECONNRESET => e logger.warn("[Woods] RedisCacheStore#read failed for #{key}: #{e.}") nil end |
#write(key, value, ttl: nil) ⇒ void
This method returns an undefined value.
Write a value to Redis with optional TTL.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/woods/cache/redis_cache_store.rb', line 59 def write(key, value, ttl: nil) serialized = JSON.generate(value) effective_ttl = ttl || @default_ttl if effective_ttl @redis.set(key, serialized, ex: effective_ttl) else @redis.set(key, serialized) end rescue ::Redis::BaseError, Errno::ECONNREFUSED, Errno::ECONNRESET => e logger.warn("[Woods] RedisCacheStore#write failed for #{key}: #{e.}") nil end |