Class: Langfuse::CacheWarmer
- Inherits:
-
Object
- Object
- Langfuse::CacheWarmer
- Defined in:
- lib/langfuse/cache_warmer.rb
Overview
Cache warming utility for pre-loading prompts into cache
Useful for deployment scenarios where you want to warm the cache before serving traffic, preventing cold-start API calls.
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
Langfuse client used for fetching prompts.
Instance Method Summary collapse
-
#cache_enabled? ⇒ Boolean
Check if cache warming is enabled.
-
#cache_stats ⇒ Hash?
Get cache statistics (if supported by backend).
-
#initialize(client: nil) ⇒ CacheWarmer
constructor
Initialize a new cache warmer.
-
#warm(prompt_names, versions: {}, labels: {}) ⇒ Hash
Warm the cache with specified prompts.
-
#warm!(prompt_names, versions: {}, labels: {}) ⇒ Hash
Warm the cache and raise on any failures.
-
#warm_all(default_label: "production", versions: {}, labels: {}) ⇒ Hash
Warm the cache with all prompts (auto-discovery).
Constructor Details
#initialize(client: nil) ⇒ CacheWarmer
Initialize a new cache warmer
29 30 31 |
# File 'lib/langfuse/cache_warmer.rb', line 29 def initialize(client: nil) @client = client || Langfuse.client end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns Langfuse client used for fetching prompts.
24 25 26 |
# File 'lib/langfuse/cache_warmer.rb', line 24 def client @client end |
Instance Method Details
#cache_enabled? ⇒ Boolean
Check if cache warming is enabled
Returns false if caching is disabled (cache_ttl = 0)
150 151 152 153 154 155 |
# File 'lib/langfuse/cache_warmer.rb', line 150 def cache_enabled? cache = client.api_client.cache return false if cache.nil? cache.ttl&.positive? || false end |
#cache_stats ⇒ Hash?
Get cache statistics (if supported by backend)
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/langfuse/cache_warmer.rb', line 160 def cache_stats cache = client.api_client.cache return nil unless cache stats = {} stats[:backend] = cache.class.name.split("::").last stats[:ttl] = cache.ttl if cache.respond_to?(:ttl) stats[:size] = cache.size if cache.respond_to?(:size) stats[:max_size] = cache.max_size if cache.respond_to?(:max_size) stats end |
#warm(prompt_names, versions: {}, labels: {}) ⇒ Hash
Warm the cache with specified prompts
Fetches each prompt and populates the cache. This is idempotent - safe to call multiple times.
58 59 60 61 62 63 64 65 66 |
# File 'lib/langfuse/cache_warmer.rb', line 58 def warm(prompt_names, versions: {}, labels: {}) results = { success: [], failed: [] } prompt_names.each do |name| warm_single_prompt(name, results, versions, labels) end results end |
#warm!(prompt_names, versions: {}, labels: {}) ⇒ Hash
Warm the cache and raise on any failures
Same as #warm but raises an error if any prompts fail to cache. Useful when you want to abort deployment if cache warming fails.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/langfuse/cache_warmer.rb', line 134 def warm!(prompt_names, versions: {}, labels: {}) results = warm(prompt_names, versions: versions, labels: labels) if results[:failed].any? failed_names = results[:failed].map { |f| f[:name] }.join(", ") raise CacheWarmingError, "Failed to cache prompts: #{failed_names}" end results end |
#warm_all(default_label: "production", versions: {}, labels: {}) ⇒ Hash
Warm the cache with all prompts (auto-discovery)
Automatically discovers all prompts in your Langfuse project via the list_prompts API and warms the cache with all of them. By default, fetches prompts with the “production” label. Useful for deployment scenarios where you want to ensure all prompts are cached without manually specifying them.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/langfuse/cache_warmer.rb', line 99 def warm_all(default_label: "production", versions: {}, labels: {}) prompt_list = client.list_prompts prompt_names = prompt_list.map { |p| p["name"] }.uniq # Build labels hash: apply default_label to all prompts, then merge overrides # BUT: if a version is specified for a prompt, don't apply a label (version takes precedence) final_labels = {} if default_label prompt_names.each do |name| # Only apply default label if no version specified for this prompt final_labels[name] = default_label unless versions[name] end end final_labels.merge!(labels) # Specific label overrides win warm(prompt_names, versions: versions, labels: final_labels) end |