cache

package
v0.0.0-...-8929951 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cache provides cache key generation and formatting for compilation results

CRITICAL INVARIANT: SORTED ORDER REQUIREMENT All cache keys MUST be generated with inputs sorted in a consistent order. This ensures that identical compilations produce identical cache keys regardless of input order.

Sorted components:

  • Proto files: sorted by Path (string comparison)
  • Dependencies: sorted by ModuleName, then Version
  • Dependency proto files: sorted by Path
  • Options map: keys sorted alphabetically before hashing

CHANGING THESE SORT ORDERS WILL INVALIDATE THE ENTIRE CACHE

Cache Key Format Version: v1 Format: {moduleName}:{version}:{language}:{pluginVersion}:{protoHash}:{optionsHash}

DO NOT modify generateProtoHash(), hashOptions(), or FormatCacheKey() without: 1. Incrementing cache format version 2. Clearing all existing cached compilations 3. Updating cache migration documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss is returned when a cache key is not found
	ErrCacheMiss = errors.New("cache miss")

	// ErrCacheUnavailable is returned when the cache is unavailable
	ErrCacheUnavailable = errors.New("cache unavailable")

	// ErrInvalidCacheKey is returned when a cache key is invalid
	ErrInvalidCacheKey = errors.New("invalid cache key")

	// ErrCacheFull is returned when the cache is full
	ErrCacheFull = errors.New("cache full")
)

Functions

func FormatCacheKey

func FormatCacheKey(key *codegen.CacheKey) string

FormatCacheKey formats a cache key as a string for storage

Cache Key Format Version: v1 Format: {moduleName}:{version}:{language}:{pluginVersion}:{protoHash}:{optionsHash}

CRITICAL: This is the single source of truth for cache key string format. The codegen.CacheKey.String() method delegates to this function.

CHANGING THIS FORMAT INVALIDATES ALL CACHED COMPILATIONS If you must change it: 1. Increment version number in package documentation 2. Clear all cached data (or implement migration) 3. Update cache documentation

func GenerateCacheKey

func GenerateCacheKey(moduleName, version, language, pluginVersion string, protoFiles []codegen.ProtoFile, dependencies []codegen.Dependency, options map[string]string) *codegen.CacheKey

GenerateCacheKey generates a cache key from compilation parameters

IMPORTANT: This function ensures deterministic key generation by sorting all inputs. DO NOT call with pre-sorted inputs assuming you're helping - the sorting is intentional and must happen in this function to guarantee consistency.

func GetKeyString

func GetKeyString(key *codegen.CacheKey) string

GetKeyString returns the cache key as a string (helper for codegen.CacheKey.String())

func ValidateCacheKey

func ValidateCacheKey(key *codegen.CacheKey) error

ValidateCacheKey validates a cache key

Types

type Config

type Config struct {
	MaxSize int64         // Max size in bytes (default: 100MB)
	TTL     time.Duration // TTL for cache entries (default: 5 minutes)
}

Config holds cache configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default cache configuration

type MemoryCache

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

MemoryCache implements a simple in-memory LRU cache

func NewCache

func NewCache(config *Config) (*MemoryCache, error)

NewCache creates a new memory-only cache

func (*MemoryCache) Close

func (c *MemoryCache) Close() error

Close releases resources

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(ctx context.Context, key *codegen.CacheKey) error

Delete removes a cached result

func (*MemoryCache) Get

Get retrieves a cached compilation result

func (*MemoryCache) Invalidate

func (c *MemoryCache) Invalidate(ctx context.Context, moduleName, version string) error

Invalidate removes all cached results for a module/version

func (*MemoryCache) Set

Set stores a compilation result in cache

func (*MemoryCache) Stats

func (c *MemoryCache) Stats(ctx context.Context) (*Stats, error)

Stats returns cache statistics

type Stats

type Stats struct {
	Hits      int64
	Misses    int64
	HitRate   float64
	ItemCount int64
}

Stats represents cache statistics

Jump to

Keyboard shortcuts

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