hashcache

package
v1.0.126 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 5 Imported by: 0

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.

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) 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) Set

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

Set stores a scan result under the given content hash.

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