Module: RubyLLM::Agents::DSL::Caching
- Included in:
- BaseAgent
- Defined in:
- lib/ruby_llm/agents/dsl/caching.rb
Overview
Caching DSL for agent response caching.
This module provides configuration methods for caching features that can be mixed into any agent class.
Constant Summary collapse
- DEFAULT_CACHE_TTL =
Default cache TTL when none specified
1.hour
Caching DSL collapse
-
#cache(ttl_or_options = nil, for: nil, key: nil) ⇒ void
Unified cache configuration method (simplified DSL).
-
#cache_enabled? ⇒ Boolean
Returns whether caching is enabled for this agent.
-
#cache_for(ttl) ⇒ void
Enables caching for this agent with explicit TTL.
-
#cache_key_excludes(*keys) ⇒ Array<Symbol>
Specifies which parameters should be excluded from the cache key.
-
#cache_key_includes(*keys) ⇒ Array<Symbol>?
Specifies which parameters should be included in the cache key.
-
#cache_ttl ⇒ ActiveSupport::Duration
Returns the cache TTL for this agent.
-
#caching_config ⇒ Hash?
Returns the complete caching configuration hash.
Instance Method Details
#cache(ttl_or_options = nil, for: nil, key: nil) ⇒ void
This method returns an undefined value.
Unified cache configuration method (simplified DSL)
Configures caching with a cleaner syntax using keyword arguments.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 62 def cache( = nil, for: nil, key: nil) # Handle positional argument (backward compatibility) if && !.is_a?(Hash) @cache_enabled = true @cache_ttl = return end # Handle keyword arguments for_duration = binding.local_variable_get(:for) if for_duration @cache_enabled = true @cache_ttl = for_duration end @cache_key_includes = Array(key) if key end |
#cache_enabled? ⇒ Boolean
Returns whether caching is enabled for this agent
Caching IS inherited from parent classes following Ruby patterns.
85 86 87 88 89 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 85 def cache_enabled? return @cache_enabled if defined?(@cache_enabled) inherited_cache_enabled end |
#cache_for(ttl) ⇒ void
This method returns an undefined value.
Enables caching for this agent with explicit TTL
This is the preferred method for enabling caching.
41 42 43 44 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 41 def cache_for(ttl) @cache_enabled = true @cache_ttl = ttl end |
#cache_key_excludes(*keys) ⇒ Array<Symbol>
Specifies which parameters should be excluded from the cache key
118 119 120 121 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 118 def cache_key_excludes(*keys) @cache_key_excludes = keys.flatten if keys.any? @cache_key_excludes || inherited_cache_key_excludes || default_cache_key_excludes end |
#cache_key_includes(*keys) ⇒ Array<Symbol>?
Specifies which parameters should be included in the cache key
By default, all options except :skip_cache and :dry_run are included. Use this to explicitly define which parameters affect caching.
107 108 109 110 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 107 def cache_key_includes(*keys) @cache_key_includes = keys.flatten if keys.any? @cache_key_includes || inherited_cache_key_includes end |
#cache_ttl ⇒ ActiveSupport::Duration
Returns the cache TTL for this agent
94 95 96 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 94 def cache_ttl @cache_ttl || inherited_cache_ttl || DEFAULT_CACHE_TTL end |
#caching_config ⇒ Hash?
Returns the complete caching configuration hash
Used by the Cache middleware to get all settings.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby_llm/agents/dsl/caching.rb', line 128 def caching_config return nil unless cache_enabled? { enabled: true, ttl: cache_ttl, key_includes: cache_key_includes, key_excludes: cache_key_excludes }.compact end |