99 "github.com/trufflesecurity/trufflehog/v3/pkg/hasher"
1010)
1111
12+ // VerificationCache is a structure that can be used to cache verification results from detectors so that a given
13+ // credential does not trigger multiple identical remote verification attempts.
1214type VerificationCache struct {
1315 metrics MetricsReporter
1416 resultCache ResultCache
@@ -17,14 +19,32 @@ type VerificationCache struct {
1719 hasher hasher.Hasher
1820}
1921
22+ // New creates a new verification cache with the provided result cache and metrics reporter. If resultCache is nil, the
23+ // verification cache will be a no-op passthrough, although it will still record relevant metrics to the provided
24+ // metrics reporter in this case.
2025func New (resultCache ResultCache , metrics MetricsReporter ) VerificationCache {
26+ if metrics == nil {
27+ metrics = & InMemoryMetrics {}
28+ }
29+
2130 return VerificationCache {
2231 metrics : metrics ,
2332 resultCache : resultCache ,
2433 hasher : hasher .NewBlake2B (),
2534 }
2635}
2736
37+ // FromData is a cache-aware facade in front of the provided detector's FromData method.
38+ //
39+ // If the verification cache's underlying result cache is nil, or verify is false, or forceCacheUpdate is true, this
40+ // method invokes the provided detector's FromData method with the provided arguments and returns the result. If the
41+ // result cache is non-nil and forceCacheUpdate is true, the result cache is updated with the results before they are
42+ // returned.
43+ //
44+ // Otherwise, the detector's FromData method is called with verify=false. The results cache is then checked for each
45+ // returned result. If there is a cache hit for each result, these cached values are all returned. Otherwise, the
46+ // detector's FromData method is called again, but with verify=true, and the results are stored in the cache and then
47+ // returned.
2848func (v * VerificationCache ) FromData (
2949 ctx context.Context ,
3050 detector detectors.Detector ,
0 commit comments