Class: Woods::Cache::CacheStore Abstract
- Inherits:
-
Object
- Object
- Woods::Cache::CacheStore
- Defined in:
- lib/woods/cache/cache_store.rb
Overview
Subclass and override all public methods.
Abstract cache store interface.
All cache backends must implement these methods. The interface is modeled after ActiveSupport::Cache::Store for familiarity but kept minimal.
Direct Known Subclasses
Instance Method Summary collapse
-
#clear(namespace: nil) ⇒ void
Clear cached entries.
-
#delete(key) ⇒ void
Delete a key from the cache.
-
#exist?(key) ⇒ Boolean
Check if a key exists and is not expired.
-
#fetch(key, ttl: nil) { ... } ⇒ Object
Read-through cache: return cached value or execute block and cache result.
-
#read(key) ⇒ Object?
Read a value from the cache.
-
#write(key, value, ttl: nil) ⇒ void
Write a value to the cache.
Instance Method Details
#clear(namespace: nil) ⇒ void
This method returns an undefined value.
Clear cached entries. If namespace is given, only clear that domain.
78 79 80 |
# File 'lib/woods/cache/cache_store.rb', line 78 def clear(namespace: nil) raise NotImplementedError end |
#delete(key) ⇒ void
This method returns an undefined value.
Delete a key from the cache.
62 63 64 |
# File 'lib/woods/cache/cache_store.rb', line 62 def delete(key) raise NotImplementedError end |
#exist?(key) ⇒ Boolean
Check if a key exists and is not expired.
70 71 72 |
# File 'lib/woods/cache/cache_store.rb', line 70 def exist?(key) raise NotImplementedError end |
#fetch(key, ttl: nil) { ... } ⇒ Object
nil is treated as a cache miss. If the wrapped operation legitimately returns nil, every call will re-execute the block. Custom backend implementers should preserve this semantic — do not return nil for keys that were written with a non-nil value. This is acceptable for the built-in use cases (embeddings and formatted context are never nil).
Read-through cache: return cached value or execute block and cache result.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/woods/cache/cache_store.rb', line 94 def fetch(key, ttl: nil) cached = read(key) return cached unless cached.nil? value = yield begin write(key, value, ttl: ttl) rescue StandardError => e logger.warn("[Woods] CacheStore#fetch write failed for #{key}: #{e.}") end value end |
#read(key) ⇒ Object?
Read a value from the cache.
44 45 46 |
# File 'lib/woods/cache/cache_store.rb', line 44 def read(key) raise NotImplementedError end |
#write(key, value, ttl: nil) ⇒ void
This method returns an undefined value.
Write a value to the cache.
54 55 56 |
# File 'lib/woods/cache/cache_store.rb', line 54 def write(key, value, ttl: nil) raise NotImplementedError end |