Class: Langfuse::PromptCache
- Inherits:
-
Object
- Object
- Langfuse::PromptCache
- Includes:
- StaleWhileRevalidate
- Defined in:
- lib/langfuse/prompt_cache.rb
Overview
Simple in-memory cache for prompt data with TTL
Thread-safe cache implementation for storing prompt responses with time-to-live expiration.
Defined Under Namespace
Classes: CacheEntry
Instance Attribute Summary collapse
-
#logger ⇒ Logger
readonly
Logger instance for error reporting.
-
#max_size ⇒ Integer
readonly
Maximum number of cache entries.
-
#stale_ttl ⇒ Integer
readonly
Stale TTL for SWR in seconds.
-
#ttl ⇒ Integer
readonly
Time-to-live in seconds.
Class Method Summary collapse
-
.build_key(name, version: nil, label: nil) ⇒ String
Build a cache key from prompt name and options.
Instance Method Summary collapse
-
#cleanup_expired ⇒ Integer
Remove expired entries from cache.
-
#clear ⇒ void
Clear the entire cache.
-
#empty? ⇒ Boolean
Check if cache is empty.
-
#get(key) ⇒ Object?
Get a value from the cache.
-
#initialize(ttl: 60, max_size: 1000, stale_ttl: 0, refresh_threads: 5, logger: default_logger) ⇒ PromptCache
constructor
Initialize a new cache.
-
#set(key, value) ⇒ Object
Set a value in the cache.
-
#size ⇒ Integer
Get current cache size.
Methods included from StaleWhileRevalidate
#fetch_with_stale_while_revalidate, #initialize_swr, #shutdown, #swr_enabled?
Constructor Details
#initialize(ttl: 60, max_size: 1000, stale_ttl: 0, refresh_threads: 5, logger: default_logger) ⇒ PromptCache
Initialize a new cache
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/langfuse/prompt_cache.rb', line 76 def initialize(ttl: 60, max_size: 1000, stale_ttl: 0, refresh_threads: 5, logger: default_logger) @ttl = ttl @max_size = max_size @stale_ttl = stale_ttl @logger = logger @cache = {} @monitor = Monitor.new @locks = {} # Track locks for in-memory locking initialize_swr(refresh_threads: refresh_threads) if swr_enabled? end |
Instance Attribute Details
#logger ⇒ Logger (readonly)
Returns Logger instance for error reporting.
66 67 68 |
# File 'lib/langfuse/prompt_cache.rb', line 66 def logger @logger end |
#max_size ⇒ Integer (readonly)
Returns Maximum number of cache entries.
60 61 62 |
# File 'lib/langfuse/prompt_cache.rb', line 60 def max_size @max_size end |
#stale_ttl ⇒ Integer (readonly)
Returns Stale TTL for SWR in seconds.
63 64 65 |
# File 'lib/langfuse/prompt_cache.rb', line 63 def stale_ttl @stale_ttl end |
#ttl ⇒ Integer (readonly)
Returns Time-to-live in seconds.
57 58 59 |
# File 'lib/langfuse/prompt_cache.rb', line 57 def ttl @ttl end |
Class Method Details
.build_key(name, version: nil, label: nil) ⇒ String
Build a cache key from prompt name and options
163 164 165 166 167 168 169 |
# File 'lib/langfuse/prompt_cache.rb', line 163 def self.build_key(name, version: nil, label: nil) key = name.to_s key += ":v#{version}" if version key += ":#{label}" if label key += ":production" unless version || label key end |
Instance Method Details
#cleanup_expired ⇒ Integer
Remove expired entries from cache
131 132 133 134 135 136 137 |
# File 'lib/langfuse/prompt_cache.rb', line 131 def cleanup_expired @monitor.synchronize do initial_size = @cache.size @cache.delete_if { |_key, entry| entry.expired? } initial_size - @cache.size end end |
#clear ⇒ void
This method returns an undefined value.
Clear the entire cache
122 123 124 125 126 |
# File 'lib/langfuse/prompt_cache.rb', line 122 def clear @monitor.synchronize do @cache.clear end end |
#empty? ⇒ Boolean
Check if cache is empty
151 152 153 154 155 |
# File 'lib/langfuse/prompt_cache.rb', line 151 def empty? @monitor.synchronize do @cache.empty? end end |
#get(key) ⇒ Object?
Get a value from the cache
91 92 93 94 95 96 97 98 99 |
# File 'lib/langfuse/prompt_cache.rb', line 91 def get(key) @monitor.synchronize do entry = @cache[key] return nil unless entry return nil if entry.expired? entry.data end end |
#set(key, value) ⇒ Object
Set a value in the cache
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/langfuse/prompt_cache.rb', line 106 def set(key, value) @monitor.synchronize do # Evict oldest entry if at max size evict_oldest if @cache.size >= max_size now = Time.now fresh_until = now + ttl stale_until = fresh_until + stale_ttl @cache[key] = CacheEntry.new(value, fresh_until, stale_until) value end end |
#size ⇒ Integer
Get current cache size
142 143 144 145 146 |
# File 'lib/langfuse/prompt_cache.rb', line 142 def size @monitor.synchronize do @cache.size end end |