Skip to content

Purl matcher cache - #2819

Open
Hubtrick-Git wants to merge 6 commits into
mainfrom
purl-matcher-cache
Open

Purl matcher cache#2819
Hubtrick-Git wants to merge 6 commits into
mainfrom
purl-matcher-cache

Conversation

@Hubtrick-Git

Copy link
Copy Markdown
Collaborator

Description

Added a cache which caches affected components for a given purl, if recently fetched from the database.
This aims to reduce fetch requests to the database on scans
Cache if flushed only after a successful vulndb import.

Discussion

The cache holds slices of affected components. Currently its initialized with 10_000 entries (arbitrary value). Also it can grow indefinitely; this could potentially lead to big memory consumption.

Ideas to solve:

  • Cap the map at the 10_000 (or other number) entries
  • Add a smart clean up logic which prioritizes hot cache entries and removes cold ones
  • Use a slice of pointers instead of a slice of values

@Hubtrick-Git
Hubtrick-Git requested review from timbastin and a lite review from Copilot and removed request for Copilot August 10, 2026 10:49
Copilot AI lite review requested due to automatic review settings August 11, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an in-memory cache for PURL-to-affected-components lookups in the vuln DB scanner to reduce repeated database fetches during scans, and flushes that cache after a successful vuln DB import so cached results don’t go stale.

Changes:

  • Introduces a generation-based, RWMutex-protected cache keyed by normalized PURL + query shape and integrates it into candidate resolution.
  • Flushes the scan cache after a successful ImportRC commit.
  • Adds unit tests covering cache keying, flush/generation behavior, and cache usage in resolver logic.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
vulndb/vulndb_service.go Flushes the scan cache after a successful vuln DB import commit.
vulndb/scan/purl_comparer.go Implements and uses an affected-components cache during PURL matching.
vulndb/scan/purl_comparer_test.go Adds tests for cache behavior, safety, and resolver cache hits.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread vulndb/scan/purl_comparer.go Outdated
Comment on lines +148 to +149
components, ok := acc.cache[candidate.cacheKey()]
return components, ok
Comment on lines +219 to 223
// only cache after every shape is finished
cache.SetForCandidates(candidates, generationOfValues)

slog.Info("finished purl matching", "cache usage", float32(cacheUsage)/float32(len(purls)))
// the candidates are in request order, whereas byShape is not
Comment thread vulndb/scan/purl_comparer_test.go
Comment thread vulndb/vulndb_service.go Outdated
Hubtrick-Git and others added 2 commits August 11, 2026 18:27
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Patrick <patrick.rissmann@l3montree.com>
Co-authored-by: Hubtrick-Git <199759119+Hubtrick-Git@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants