Class: RailsAiBridge::Registry::ResolverCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/registry/resolver_cache.rb

Overview

Thread-safe resolver cache with configurable TTL.

Single responsibility: holds one resolved +Resolver+ and tracks its age. Callers call #fetch with a build block; the cache returns the warm resolver when the TTL has not expired, or calls the block to rebuild.

A nil result from the block (manifest missing) is never cached so the next call will try again — useful during initial setup.

Examples:

cache = ResolverCache.new
resolver = cache.fetch(config) { Registry.build_resolver_uncached(config) }

Instance Method Summary collapse

Constructor Details

#initialize(monotonic_clock: method(:default_clock)) ⇒ ResolverCache

Returns a new instance of ResolverCache.

Parameters:

  • monotonic_clock (#call) (defaults to: method(:default_clock))

    injectable clock for testing; defaults to Process.clock_gettime



19
20
21
22
23
24
# File 'lib/rails_ai_bridge/registry/resolver_cache.rb', line 19

def initialize(monotonic_clock: method(:default_clock))
  @mutex   = Mutex.new
  @clock   = monotonic_clock
  @entry   = nil
  @built_at = nil
end

Instance Method Details

#fetch(config, &build_block) ⇒ Registry::Resolver?

Return a warm cached resolver or build a new one.

Parameters:

Yield Returns:

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_ai_bridge/registry/resolver_cache.rb', line 31

def fetch(config, &build_block)
  @mutex.synchronize do
    if @entry && !expired?(config)
      @entry
    else
      result = build_block.call
      if result
        @entry    = result
        @built_at = @clock.call
      end
      result
    end
  end
end

#invalidate!void

This method returns an undefined value.

Discard the cached resolver; the next #fetch call will rebuild.



48
49
50
51
52
53
# File 'lib/rails_ai_bridge/registry/resolver_cache.rb', line 48

def invalidate!
  @mutex.synchronize do
    @entry    = nil
    @built_at = nil
  end
end