Class: RailsAiBridge::Fingerprinter::CachedSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/fingerprinter/cached_snapshot.rb

Overview

Caches fingerprint snapshots to avoid redundant filesystem walks.

Every +ContextProvider.fetch+ triggers a +Fingerprinter.snapshot+ call that walks 10+ directories reading mtimes. This class caches the result for a short TTL (default 5s), so rapid MCP tool calls reuse the same snapshot without repeated I/O.

Thread-safe via +Mutex+.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.snapshot_ttlInteger

Returns seconds to cache a fingerprint snapshot.

Returns:

  • (Integer)

    seconds to cache a fingerprint snapshot



20
21
22
# File 'lib/rails_ai_bridge/fingerprinter/cached_snapshot.rb', line 20

def snapshot_ttl
  @snapshot_ttl
end

Class Method Details

.fetch(app) ⇒ String

Returns the fingerprint for +app+, reusing a cached value when the snapshot TTL has not expired.

Parameters:

  • app (Rails::Application)

Returns:

  • (String)

    SHA256 hex digest



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_ai_bridge/fingerprinter/cached_snapshot.rb', line 27

def fetch(app)
  @mutex.synchronize do
    key = app.object_id
    entry = @cache[key]

    if entry && ttl_valid?(entry)
      entry[:fingerprint]
    else
      compute_and_cache(app, key)
    end
  end
end

.invalidate!(app) ⇒ void

This method returns an undefined value.

Invalidates the cached snapshot for a specific app, forcing re-computation on the next +fetch+.

Parameters:

  • app (Rails::Application)


52
53
54
# File 'lib/rails_ai_bridge/fingerprinter/cached_snapshot.rb', line 52

def invalidate!(app)
  @mutex.synchronize { @cache.delete(app.object_id) }
end

.reset!void

This method returns an undefined value.

Clears all cached snapshots.



43
44
45
# File 'lib/rails_ai_bridge/fingerprinter/cached_snapshot.rb', line 43

def reset!
  @mutex.synchronize { @cache.clear }
end