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 ⇒ Object
readonly
Returns the value of attribute logger.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#stale_ttl ⇒ Object
readonly
Returns the value of attribute stale_ttl.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
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
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/langfuse/prompt_cache.rb', line 66 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 ⇒ Object (readonly)
Returns the value of attribute logger.
56 57 58 |
# File 'lib/langfuse/prompt_cache.rb', line 56 def logger @logger end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
56 57 58 |
# File 'lib/langfuse/prompt_cache.rb', line 56 def max_size @max_size end |
#stale_ttl ⇒ Object (readonly)
Returns the value of attribute stale_ttl.
56 57 58 |
# File 'lib/langfuse/prompt_cache.rb', line 56 def stale_ttl @stale_ttl end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
56 57 58 |
# File 'lib/langfuse/prompt_cache.rb', line 56 def ttl @ttl end |
Class Method Details
.build_key(name, version: nil, label: nil) ⇒ String
Build a cache key from prompt name and options
153 154 155 156 157 158 |
# File 'lib/langfuse/prompt_cache.rb', line 153 def self.build_key(name, version: nil, label: nil) key = name.to_s key += ":v#{version}" if version key += ":#{label}" if label key end |
Instance Method Details
#cleanup_expired ⇒ Integer
Remove expired entries from cache
121 122 123 124 125 126 127 |
# File 'lib/langfuse/prompt_cache.rb', line 121 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
112 113 114 115 116 |
# File 'lib/langfuse/prompt_cache.rb', line 112 def clear @monitor.synchronize do @cache.clear end end |
#empty? ⇒ Boolean
Check if cache is empty
141 142 143 144 145 |
# File 'lib/langfuse/prompt_cache.rb', line 141 def empty? @monitor.synchronize do @cache.empty? end end |
#get(key) ⇒ Object?
Get a value from the cache
81 82 83 84 85 86 87 88 89 |
# File 'lib/langfuse/prompt_cache.rb', line 81 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
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/langfuse/prompt_cache.rb', line 96 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
132 133 134 135 136 |
# File 'lib/langfuse/prompt_cache.rb', line 132 def size @monitor.synchronize do @cache.size end end |