Class: Woods::Cache::SolidCacheStore
- Inherits:
-
CacheStore
- Object
- CacheStore
- Woods::Cache::SolidCacheStore
- Defined in:
- lib/woods/cache/solid_cache_store.rb
Overview
SolidCache-backed (or any ActiveSupport::Cache::Store) cache store.
Delegates to a Rails-compatible cache backend. Values are JSON-serialized to avoid Marshal dependency issues across Ruby versions. TTL is passed as ‘expires_in:` to the underlying cache.
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 the cache.
-
#exist?(key) ⇒ Boolean
Check if a key exists in the cache.
-
#initialize(cache:, default_ttl: nil) ⇒ SolidCacheStore
constructor
A new instance of SolidCacheStore.
-
#read(key) ⇒ Object?
Read a value from the cache.
-
#write(key, value, ttl: nil) ⇒ void
Write a value with optional TTL.
Methods inherited from CacheStore
Constructor Details
#initialize(cache:, default_ttl: nil) ⇒ SolidCacheStore
Returns a new instance of SolidCacheStore.
25 26 27 28 29 |
# File 'lib/woods/cache/solid_cache_store.rb', line 25 def initialize(cache:, default_ttl: nil) super() @cache = cache @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 ‘delete_matched` if the underlying cache supports it (Redis, Memcached). Falls back to a no-op if pattern deletion is not available (some backends like SolidCache don’t support wildcard deletion).
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/woods/cache/solid_cache_store.rb', line 95 def clear(namespace: nil) pattern = clear_pattern(namespace) unless @cache.respond_to?(:delete_matched) logger.warn("[Woods] Cache#clear(namespace: #{namespace.inspect}) is a no-op: " \ "backend #{@cache.class} does not support delete_matched") return end @cache.delete_matched(pattern) rescue StandardError => e logger.warn("[Woods] SolidCacheStore#clear failed: #{e.}") nil end |
#delete(key) ⇒ void
This method returns an undefined value.
Delete a key from the cache.
69 70 71 72 73 74 |
# File 'lib/woods/cache/solid_cache_store.rb', line 69 def delete(key) @cache.delete(key) rescue StandardError => e logger.warn("[Woods] SolidCacheStore#delete failed for #{key}: #{e.}") nil end |
#exist?(key) ⇒ Boolean
Check if a key exists in the cache.
80 81 82 83 84 85 |
# File 'lib/woods/cache/solid_cache_store.rb', line 80 def exist?(key) @cache.exist?(key) rescue StandardError => e logger.warn("[Woods] SolidCacheStore#exist? failed for #{key}: #{e.}") false end |
#read(key) ⇒ Object?
Read a value from the cache.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/woods/cache/solid_cache_store.rb', line 35 def read(key) raw = @cache.read(key) return nil unless raw JSON.parse(raw) rescue JSON::ParserError delete_silently(key) nil rescue StandardError => e logger.warn("[Woods] SolidCacheStore#read failed for #{key}: #{e.}") nil end |
#write(key, value, ttl: nil) ⇒ void
This method returns an undefined value.
Write a value with optional TTL.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/woods/cache/solid_cache_store.rb', line 54 def write(key, value, ttl: nil) serialized = JSON.generate(value) effective_ttl = ttl || @default_ttl opts = effective_ttl ? { expires_in: effective_ttl } : {} @cache.write(key, serialized, **opts) rescue StandardError => e logger.warn("[Woods] SolidCacheStore#write failed for #{key}: #{e.}") nil end |