Module: SolidAgent::ToolCache
- Defined in:
- lib/solid_agent/tool_cache.rb
Constant Summary collapse
- DEFAULT_TTL =
seconds
300
Class Attribute Summary collapse
Class Method Summary collapse
- .cache_key(tool, args) ⇒ Object
-
.fetch(tool:, args: {}, ttl: nil, cache: store) ⇒ Object
Returns the cached result for (tool, args) or yields, caching the fresh result.
Class Attribute Details
.default_ttl ⇒ Object
34 35 36 |
# File 'lib/solid_agent/tool_cache.rb', line 34 def default_ttl @default_ttl || DEFAULT_TTL end |
.enabled ⇒ Object
30 31 32 |
# File 'lib/solid_agent/tool_cache.rb', line 30 def enabled defined?(@enabled) ? @enabled : true end |
.store ⇒ Object
38 39 40 |
# File 'lib/solid_agent/tool_cache.rb', line 38 def store @store || (defined?(Rails) && Rails.respond_to?(:cache) ? Rails.cache : nil) end |
Class Method Details
.cache_key(tool, args) ⇒ Object
60 61 62 63 |
# File 'lib/solid_agent/tool_cache.rb', line 60 def cache_key(tool, args) digest = Digest::SHA256.hexdigest(normalize_args(args).to_json) "solid_agent:tool_cache:#{tool}:#{digest}" end |
.fetch(tool:, args: {}, ttl: nil, cache: store) ⇒ Object
Returns the cached result for (tool, args) or yields, caching the fresh result. Results that look like errors ({ error: ... }) are never cached, so transient failures don't stick.
The returned hash is tagged with cached: true on cache hits so callers (and the model) can tell a replayed result from a fresh one.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/solid_agent/tool_cache.rb', line 48 def fetch(tool:, args: {}, ttl: nil, cache: store) return yield unless enabled && cache key = cache_key(tool, args) cached = cache.read(key) return tag_cached(cached) unless cached.nil? result = yield cache.write(key, result, expires_in: ttl || default_ttl) if cacheable?(result) result end |