Class: DSPy::LM::CacheManager

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/lm/cache_manager.rb

Overview

Manages caching for schemas and capability detection

Defined Under Namespace

Classes: CacheEntry

Constant Summary collapse

DEFAULT_TTL =

1 hour

3600

Instance Method Summary collapse

Constructor Details

#initializeCacheManager

Returns a new instance of CacheManager.



27
28
29
30
31
# File 'lib/dspy/lm/cache_manager.rb', line 27

def initialize
  @schema_cache = {}
  @capability_cache = {}
  @mutex = Mutex.new
end

Instance Method Details

#cache_capability(model, capability, result) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dspy/lm/cache_manager.rb', line 69

def cache_capability(model, capability, result)
  key = capability_key(model, capability)
  
  @mutex.synchronize do
    @capability_cache[key] = CacheEntry.new(
      value: result,
      expires_at: Time.now + DEFAULT_TTL * 24 # Capabilities change less frequently
    )
  end
  
  DSPy.logger.debug("Cached capability #{capability} for #{model}: #{result}")
end

#cache_schema(signature_class, provider, schema, cache_params = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dspy/lm/cache_manager.rb', line 35

def cache_schema(signature_class, provider, schema, cache_params = {})
  key = schema_key(signature_class, provider, cache_params)
  
  @mutex.synchronize do
    @schema_cache[key] = CacheEntry.new(
      value: schema,
      expires_at: Time.now + DEFAULT_TTL
    )
  end
  
  DSPy.logger.debug("Cached schema for #{signature_class.name} (#{provider})")
end

#clear!Object



103
104
105
106
107
108
109
110
# File 'lib/dspy/lm/cache_manager.rb', line 103

def clear!
  @mutex.synchronize do
    @schema_cache.clear
    @capability_cache.clear
  end
  
  DSPy.logger.debug("Cleared all caches")
end

#get_capability(model, capability) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dspy/lm/cache_manager.rb', line 84

def get_capability(model, capability)
  key = capability_key(model, capability)
  
  @mutex.synchronize do
    entry = @capability_cache[key]
    
    if entry.nil?
      nil
    elsif entry.expired?
      @capability_cache.delete(key)
      nil
    else
      entry.value
    end
  end
end

#get_schema(signature_class, provider, cache_params = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dspy/lm/cache_manager.rb', line 50

def get_schema(signature_class, provider, cache_params = {})
  key = schema_key(signature_class, provider, cache_params)
  
  @mutex.synchronize do
    entry = @schema_cache[key]
    
    if entry.nil?
      nil
    elsif entry.expired?
      @schema_cache.delete(key)
      nil
    else
      entry.value
    end
  end
end

#statsObject



114
115
116
117
118
119
120
121
122
# File 'lib/dspy/lm/cache_manager.rb', line 114

def stats
  @mutex.synchronize do
    {
      schema_entries: @schema_cache.size,
      capability_entries: @capability_cache.size,
      total_entries: @schema_cache.size + @capability_cache.size
    }
  end
end