hashcache

package
v1.0.407 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hashcache provides a size-bounded, TTL-evicting cache of security scan results keyed on the SHA-256 digest of scanned content. It is a self-contained leaf (stdlib only, no Culvert coupling) extracted from the flat package main per ADR-0002.

Avoids redundant ClamAV / YARA scans by caching the outcome of each scan keyed on the SHA-256 digest of the scanned content. The same executable or document delivered from multiple hosts is therefore scanned only once per TTL window, dramatically reducing CPU load on busy proxies.

Design:

  • Fixed-capacity map with TTL expiry.
  • On capacity overflow: expired entries are evicted first; if still full, ~25 % of entries are dropped (simple, avoids per-entry LRU bookkeeping).
  • All operations are mutex-protected; hit/miss counters use atomic int64.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SHA256Hex

func SHA256Hex(data []byte) string

SHA256Hex computes the SHA-256 digest of data and returns it as a lowercase hex string. Used as the cache key for scanned content.

Types

type HashCache

type HashCache struct {
	// contains filtered or unexported fields
}

HashCache is a size-bounded, TTL-evicting cache of SHA-256 scan results.

func New

func New(maxSize int, ttl time.Duration) *HashCache

New returns a HashCache with the given capacity and TTL. Sensible defaults are used when size ≤ 0 or ttl ≤ 0.

func (*HashCache) Clear

func (c *HashCache) Clear()

Clear removes all entries from the cache and advances its invalidation epoch, so a SetIfGeneration for a verdict begun before the Clear is refused.

func (*HashCache) Evict

func (c *HashCache) Evict(hash string) bool

Evict removes a specific hash from the cache. Returns true if the entry existed (regardless of expiry).

func (*HashCache) Generation added in v1.0.382

func (c *HashCache) Generation() uint64

Generation returns the cache's current invalidation epoch, for a caller that is about to compute a verdict and must not publish it if the cache is invalidated before it finishes (see SetIfGeneration).

func (*HashCache) Get

func (c *HashCache) Get(hash string) (ScanCacheResult, bool)

Get retrieves a cached result for the given hash. Returns (result, true) on a valid, non-expired cache hit.

func (*HashCache) Invalidate added in v1.0.382

func (c *HashCache) Invalidate() (dropped int)

Invalidate is Clear that also reports how many entries it dropped. The epoch advances even when the cache is empty: an empty cache says nothing about verdicts still being COMPUTED, and those are exactly what the epoch exists to refuse. The count is read under the same lock as the drop, so it is exact.

func (*HashCache) Set

func (c *HashCache) Set(hash string, result ScanCacheResult)

Set stores a scan result under the given content hash, with the cache's configured TTL.

func (*HashCache) SetIfGeneration added in v1.0.382

func (c *HashCache) SetIfGeneration(hash string, result ScanCacheResult, gen uint64) bool

SetIfGeneration stores result with the cache's configured TTL only if no Clear has happened since gen was obtained from Generation, and reports whether it stored.

It exists for verdicts that take time to compute. A Clear means "everything computed before this point is no longer trusted" — new signatures, changed rules, an operator flush — and a verdict whose computation STARTED before the Clear but finished after it belongs to the distrusted side. A plain Set would land it in the freshly cleared cache, where nothing would ever invalidate it again for the rest of the TTL. The generation compare and the store happen under the same write lock Clear takes, so there is no window in which a Clear can slip between them.

func (*HashCache) SetTTL added in v1.0.209

func (c *HashCache) SetTTL(hash string, result ScanCacheResult, ttl time.Duration)

SetTTL stores a scan result with an explicit lifetime; ttl ≤ 0 uses the cache's configured TTL.

It exists for verdicts that are ABOUT THE SCANNER rather than about the content — a fail-closed scan-timeout refusal, for instance. Those must not inherit the content TTL: a scanner that was briefly slow would otherwise keep blocking a specific object for the rest of the hour, long after it recovered.

func (*HashCache) SetTTLUnless added in v1.0.209

func (c *HashCache) SetTTLUnless(hash string, result ScanCacheResult, ttl time.Duration, keep func(existing ScanCacheResult) bool) bool

SetTTLUnless stores result under hash with the given lifetime unless keep reports that the entry already present must be preserved. It returns whether the write happened; ttl ≤ 0 uses the cache's configured TTL.

The test and the write are one atomic step under the cache lock. A caller doing Get-then-Set instead would leave a window in which a stronger verdict lands between the two and is overwritten anyway — which is the exact race this exists to close. keep is called only with a present, unexpired entry (an expired one counts as absent), and hit/miss counters are untouched: this is a write path, not a lookup.

func (*HashCache) Stats

func (c *HashCache) Stats() (hits, misses int64, currentSize int)

Stats returns (hits, misses, currentSize) for Prometheus / admin UI.

type ScanCacheResult

type ScanCacheResult struct {
	Clean     bool   // true = no threat detected
	Reason    string // virus name or YARA rule name when not clean
	Source    string // "clamav", "yara", or "clean"
	ScannedAt time.Time
}

ScanCacheResult records the outcome of a security scan for a content hash.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL