cache

package
v0.0.0-...-6b1ce1c Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package cache provides file-based caching for parsed AST nodes.

This package enables incremental duplicate detection by caching the parsed AST nodes for each file. When a file hasn't changed (same content hash), its cached AST can be reused instead of re-parsing.

Cache Structure:

.cache/art-dupl/
├── files/
│   ├── <hash1>.gob    # Serialized AST nodes for file with hash1
│   ├── <hash2>.gob    # Serialized AST nodes for file with hash2
│   └── ...
└── metadata.json      # Cache metadata (version, timestamps)

Usage:

cache := cache.NewFileCache(".cache/art-dupl")
nodes, hit := cache.Get(fileHash)
if !hit {
    nodes = parseFile(path)
    cache.Set(fileHash, nodes)
}

Index

Constants

View Source
const (
	// DefaultCacheDir is the default directory name for the cache.
	DefaultCacheDir = ".cache/art-dupl"

	// CacheVersion is incremented when cache format changes.
	CacheVersion = 1
)

Variables

This section is empty.

Functions

func CacheKey deprecated

func CacheKey(content []byte) string

Key generates a cache key from file content. This uses SHA1 for cache keys (fast and collision-resistant enough for this use case).

Deprecated: Use Key instead. CacheKey is kept for backward compatibility.

func Key

func Key(content []byte) string

Key generates a cache key from file content. This uses SHA1 for cache keys (fast and collision-resistant enough for this use case).

Types

type FileCache

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

FileCache provides caching for parsed AST nodes.

func NewFileCache

func NewFileCache(cacheDir string) *FileCache

NewFileCache creates a new FileCache. If cacheDir is empty, uses DefaultCacheDir.

func (*FileCache) Clear

func (fc *FileCache) Clear() error

Clear removes all cache entries.

func (*FileCache) Get

func (fc *FileCache) Get(contentHash string) ([]*syntax.Node, bool)

Get retrieves cached AST nodes for the given content hash. Returns the nodes and true if found (cache hit), nil and false otherwise.

func (*FileCache) GetStats

func (fc *FileCache) GetStats() (int64, int64)

GetStats returns cache hit/miss statistics.

func (*FileCache) Has

func (fc *FileCache) Has(contentHash string) bool

Has checks if a cache entry exists for the given hash.

func (*FileCache) Remove

func (fc *FileCache) Remove(contentHash string) error

Remove deletes a cache entry.

func (*FileCache) Set

func (fc *FileCache) Set(contentHash string, nodes []*syntax.Node) error

Set stores AST nodes for the given content hash.

func (*FileCache) Stats

func (fc *FileCache) Stats() Stats

Stats returns cache statistics.

type Metadata

type Metadata struct {
	Version   int       `json:"version"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	HitCount  int64     `json:"hitCount"`  // Accessed atomically
	MissCount int64     `json:"missCount"` // Accessed atomically
}

Metadata contains cache metadata.

type Stats

type Stats struct {
	Hits      int64
	Misses    int64
	Size      int // Number of cached entries
	BytesUsed int64
}

Stats returns cache statistics.

Jump to

Keyboard shortcuts

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