Module: RubyLLM::Agents::CacheHelper Private
- Included in:
- BaseAgent, Budget::BudgetQuery, Budget::SpendRecorder, BudgetTracker, CircuitBreaker
- Defined in:
- lib/ruby_llm/agents/infrastructure/cache_helper.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Shared cache utilities for RubyLLM::Agents
Provides consistent cache key generation and store access across BudgetTracker, CircuitBreaker, and Base caching modules.
Constant Summary collapse
- NAMESPACE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Cache key namespace prefix
"ruby_llm_agents"
Instance Method Summary collapse
-
#cache_delete(key) ⇒ Boolean
private
Deletes a key from the cache.
-
#cache_exist?(key) ⇒ Boolean
private
Checks if a key exists in the cache.
-
#cache_increment(key, amount = 1, expires_in: nil) ⇒ Numeric
private
Increments a numeric value in the cache.
-
#cache_key(*parts) ⇒ String
private
Generates a namespaced cache key from the given parts.
-
#cache_read(key) ⇒ Object?
private
Reads a value from the cache.
-
#cache_store ⇒ ActiveSupport::Cache::Store
private
Returns the configured cache store.
-
#cache_write(key, value, **options) ⇒ Boolean
private
Writes a value to the cache.
Instance Method Details
#cache_delete(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Deletes a key from the cache
71 72 73 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 71 def cache_delete(key) cache_store.delete(key) end |
#cache_exist?(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if a key exists in the cache
63 64 65 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 63 def cache_exist?(key) cache_store.exist?(key) end |
#cache_increment(key, amount = 1, expires_in: nil) ⇒ Numeric
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Increments a numeric value in the cache
Falls back to read-modify-write if the cache store doesn’t support increment.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 83 def cache_increment(key, amount = 1, expires_in: nil) if cache_store.respond_to?(:increment) # Ensure key exists with TTL cache_store.write(key, 0, expires_in: expires_in, unless_exist: true) if expires_in cache_store.increment(key, amount) else # Fallback for cache stores without atomic increment current = (cache_store.read(key) || 0).to_f new_value = current + amount cache_store.write(key, new_value, expires_in: expires_in) new_value end end |
#cache_key(*parts) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a namespaced cache key from the given parts
37 38 39 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 37 def cache_key(*parts) ([NAMESPACE] + parts.map(&:to_s)).join(":") end |
#cache_read(key) ⇒ Object?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reads a value from the cache
45 46 47 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 45 def cache_read(key) cache_store.read(key) end |
#cache_store ⇒ ActiveSupport::Cache::Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the configured cache store
26 27 28 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 26 def cache_store RubyLLM::Agents.configuration.cache_store end |
#cache_write(key, value, **options) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Writes a value to the cache
55 56 57 |
# File 'lib/ruby_llm/agents/infrastructure/cache_helper.rb', line 55 def cache_write(key, value, **) cache_store.write(key, value, **) end |