cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const CacheFileName = "incremental.cache"

Variables

This section is empty.

Functions

func Clear

func Clear(cacheFilePath string) error

Clear removes the cache file at the given path.

func ClearDir

func ClearDir(dir string) error

ClearDir removes the cache file from the given directory (legacy API).

func ClearSharedCache

func ClearSharedCache(cacheDir string) error

ClearSharedCache removes all cache files from a shared cache directory.

func ComputeConfigHash

func ComputeConfigHash(ruleNames []string, cfg *config.Config, editorConfigEnabled bool) string

ComputeConfigHash computes a hash from active rule names, the resolved config, and whether editorconfig is enabled. This ensures the cache is invalidated when config-derived thresholds change (e.g., MaxLineLength from editorconfig).

func ComputeFileHash

func ComputeFileHash(path string) string

ComputeFileHash computes a SHA-256 hash of the file content (truncated to 16 hex chars). Routed through the shared hashutil.Memo so a file hashed by other cache subsystems in the same run pays SHA-256 once.

func ComputeRuleHash

func ComputeRuleHash(ruleNames []string) string

ComputeRuleHash computes a hash from the sorted list of active rule names. This ensures cache invalidation when rules change. Deprecated: Use ComputeConfigHash for config-aware cache invalidation.

func DefaultDir

func DefaultDir(repoDir string) string

DefaultDir returns Krit's repo-local incremental cache directory.

func FilePath

func FilePath(cacheDir string, scanPaths []string) string

FilePath returns the full path to the cache file for the given directory and scan paths. When cacheDir is non-empty and scanPaths are provided, the filename is derived from a hash of the scan paths to avoid collisions.

func NeedsReanalysis

func NeedsReanalysis(path string, entry FileEntry) bool

NeedsReanalysis checks whether a file needs to be re-analyzed. Fast path: compare modtime + size. Slow path: content hash.

func ParseRuleSetHash

func ParseRuleSetHash(hexStr string) [16]byte

ParseRuleSetHash converts a 32-hex-char config hash string (from ComputeConfigHash) into the 16-byte form used in store.Key.

func ResolveCacheDir

func ResolveCacheDir(cacheDir string, scanPaths []string) (dir string, filePath string)

ResolveCacheDir returns the cache directory and file path to use. If cacheDir is set via --cache-dir, it is used with a hashed filename. Otherwise Krit writes under the repo-local .krit/cache directory.

Types

type Cache

type Cache struct {
	Version   string               `json:"version"`
	RuleHash  string               `json:"ruleHash"`
	ScanPaths []string             `json:"scanPaths,omitempty"`
	Files     map[string]FileEntry `json:"files"`
	// contains filtered or unexported fields
}

Cache holds the entire incremental analysis cache.

func Load

func Load(cacheFilePath string) *Cache

Load reads the cache file from the given path. Returns an empty cache if the file does not exist or is invalid.

func LoadFromDir

func LoadFromDir(dir string) *Cache

LoadFromDir reads the cache file from the given directory (legacy API). Deprecated: Use Load with a full file path from ResolveCacheDir.

func (*Cache) AttachStore

func (c *Cache) AttachStore(s *store.FileStore, ruleSetHash [16]byte)

AttachStore configures c to read and write all incremental cache entries through s instead of the in-memory Files map. ruleSetHash must be the parsed form of ComputeConfigHash (use ParseRuleSetHash).

Once attached, CheckFiles consults the store; UpdateEntryColumns writes to the store; Save becomes a no-op. The existing JSON file is no longer read after AttachStore — the first warm run regenerates entries in the store.

func (*Cache) CheckFiles

func (c *Cache) CheckFiles(filePaths []string, ruleHash string, scanPaths ...string) *Result

CheckFiles checks which files can use cached results and which need reanalysis. Returns cached findings and a set of paths that are cache hits. When scanPaths is non-empty, the cache is invalidated if the scan paths differ.

func (*Cache) Prune

func (c *Cache) Prune()

Prune removes entries for files that no longer exist.

func (*Cache) Save

func (c *Cache) Save(cacheFilePath string) error

Save writes the cache file atomically to the given path. It writes to a temporary file first, then renames for crash safety and safe concurrent access. When a store is attached, Save is a no-op because entries are persisted individually on each UpdateEntry call.

func (*Cache) SaveToDir

func (c *Cache) SaveToDir(dir string) error

SaveToDir writes the cache file to the given directory (legacy API). Deprecated: Use Save with a full file path from ResolveCacheDir.

func (*Cache) ShouldSkipFullSaveForSmallDelta

func (c *Cache) ShouldSkipFullSaveForSmallDelta(result *Result, maxMisses int) bool

ShouldSkipFullSaveForSmallDelta reports whether a JSON-backed cache run should avoid rewriting the full cache file after a tiny dirty worktree delta. Dirty files are still reanalyzed on the next run unless their entry was persisted elsewhere, so this trades repeated dirty-file hits for lower save latency without reusing stale findings.

func (*Cache) UpdateEntryColumns

func (c *Cache) UpdateEntryColumns(path string, columns *scanner.FindingColumns)

UpdateEntryColumns updates the cache for a single file after analysis using columnar findings without reconstituting []Finding.

type FileEntry

type FileEntry struct {
	Hash    string                 `json:"hash"`
	ModTime int64                  `json:"modTime"`
	Size    int64                  `json:"size"`
	Columns scanner.FindingColumns `json:"-"`
}

FileEntry holds cached analysis results for a single file.

func (FileEntry) MarshalJSON

func (e FileEntry) MarshalJSON() ([]byte, error)

MarshalJSON persists the cache in columnar form.

func (*FileEntry) UnmarshalJSON

func (e *FileEntry) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts both the new columnar cache encoding and the legacy findings array so older cache files still load cleanly.

type Result

type Result struct {
	CachedColumns      scanner.FindingColumns
	CachedPaths        map[string]bool // paths that were cache hits
	CachedHashes       map[string]string
	TotalCached        int
	TotalFiles         int
	GitDirtyPathsKnown bool
	GitDirtyPathCount  int
}

Result holds the outcome of checking files against the cache.

type Stats

type Stats struct {
	HitRate   float64 `json:"hitRate"`
	Cached    int     `json:"cached"`
	Total     int     `json:"total"`
	LoadDurMs int64   `json:"loadDurationMs,omitempty"`
	SaveDurMs int64   `json:"saveDurationMs,omitempty"`
}

Stats records cache hit/miss statistics for reporting.

Jump to

Keyboard shortcuts

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