cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package cache provides in-memory and disk caching for NuGet operations.

Index

Constants

View Source
const (
	// HashLength is the number of bytes to use from the SHA256 hash.
	// Set to 20 to maintain backwards compatibility with SHA-1 length.
	HashLength = 20

	// BufferSize for file I/O operations (matches NuGet.Client).
	BufferSize = 8192

	// CacheFileExtension for final cache files.
	CacheFileExtension = ".dat"

	// NewFileExtension for temporary files during atomic write.
	NewFileExtension = ".dat-new"
)

Variables

This section is empty.

Functions

func ComputeHash

func ComputeHash(value string, addIdentifiableCharacters bool) string

ComputeHash computes a hash for the given value. Matches NuGet.Client's CachingUtility.ComputeHash exactly.

func RemoveInvalidFileNameChars

func RemoveInvalidFileNameChars(value string) string

RemoveInvalidFileNameChars replaces invalid filename characters with underscores. Matches NuGet.Client's CachingUtility.RemoveInvalidFileNameChars.

func WithCacheContext

func WithCacheContext(ctx context.Context, cacheCtx *SourceCacheContext) context.Context

WithCacheContext adds the source cache context to the Go context. This allows protocol layer code to respect cache control flags without passing SourceCacheContext through every function.

Types

type DiskCache

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

DiskCache provides persistent caching to disk.

func NewDiskCache

func NewDiskCache(rootDir string, maxSize int64) (*DiskCache, error)

NewDiskCache creates a new disk cache. If rootDir is empty, cache operations are disabled (NoCache mode).

func (*DiskCache) Clear

func (dc *DiskCache) Clear() error

Clear removes all cache entries.

func (*DiskCache) Delete

func (dc *DiskCache) Delete(sourceURL string, cacheKey string) error

Delete removes a cache entry.

func (*DiskCache) Get

func (dc *DiskCache) Get(sourceURL string, cacheKey string, maxAge time.Duration) (io.ReadCloser, bool, error)

Get retrieves a cached file if it exists and is not expired. Returns (reader, true) if found and valid, (nil, false) otherwise. In NoCache mode (empty rootDir), always returns cache miss.

func (*DiskCache) GetCachePath

func (dc *DiskCache) GetCachePath(sourceURL string, cacheKey string) (cacheFile string, newFile string)

GetCachePath computes the cache file path for a source URL and cache key. Matches NuGet.Client's HttpCacheUtility.InitializeHttpCacheResult.

func (*DiskCache) Set

func (dc *DiskCache) Set(sourceURL string, cacheKey string, data io.Reader, validate func(io.ReadSeeker) error) error

Set writes data to the cache using atomic two-phase update. Matches NuGet.Client's HttpCacheUtility.CreateCacheFileAsync. In NoCache mode (empty rootDir), this is a no-op.

type Entry

type Entry struct {
	Value  []byte
	Expiry time.Time
	Size   int
	// contains filtered or unexported fields
}

Entry represents a cached value with metadata.

func (*Entry) IsExpired

func (e *Entry) IsExpired() bool

IsExpired checks if the entry has exceeded its TTL.

type MemoryCache

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

MemoryCache is an LRU cache with TTL support.

func NewMemoryCache

func NewMemoryCache(maxEntries int, maxSize int64) *MemoryCache

NewMemoryCache creates a new LRU memory cache.

func (*MemoryCache) Clear

func (mc *MemoryCache) Clear()

Clear removes all entries from the cache. This matches NuGet.Client's RefreshMemoryCache behavior.

func (*MemoryCache) Delete

func (mc *MemoryCache) Delete(key string)

Delete removes a key from the cache.

func (*MemoryCache) Get

func (mc *MemoryCache) Get(key string) ([]byte, bool)

Get retrieves a value from the cache. Returns (value, true) if found and not expired, (nil, false) otherwise.

func (*MemoryCache) Set

func (mc *MemoryCache) Set(key string, value []byte, ttl time.Duration)

Set adds or updates a value in the cache.

func (*MemoryCache) Stats

func (mc *MemoryCache) Stats() Stats

Stats returns cache statistics.

type MultiTierCache

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

MultiTierCache combines memory (L1) and disk (L2) caching with automatic promotion. When data is found in L2, it's promoted to L1 for faster subsequent access.

func NewMultiTierCache

func NewMultiTierCache(l1 *MemoryCache, l2 *DiskCache) *MultiTierCache

NewMultiTierCache creates a new multi-tier cache combining memory and disk layers.

func (*MultiTierCache) Clear

func (mtc *MultiTierCache) Clear() error

Clear clears both caches.

func (*MultiTierCache) Get

func (mtc *MultiTierCache) Get(ctx context.Context, sourceURL string, cacheKey string, maxAge time.Duration) ([]byte, bool, error)

Get retrieves from L1 first, then L2, promoting to L1 on L2 hit.

func (*MultiTierCache) Set

func (mtc *MultiTierCache) Set(ctx context.Context, sourceURL string, cacheKey string, data io.Reader, maxAge time.Duration, validate func(io.ReadSeeker) error) error

Set writes to both L1 and L2.

type SourceCacheContext

type SourceCacheContext struct {
	// MaxAge is the maximum age for cached entries (default: 30 minutes)
	MaxAge time.Duration

	// NoCache bypasses the global disk cache if true
	NoCache bool

	// DirectDownload skips cache writes (read-only mode)
	DirectDownload bool

	// RefreshMemoryCache forces in-memory cache reload
	RefreshMemoryCache bool

	// SessionID is a unique identifier for the session (X-NuGet-Session-Id header)
	SessionID string
}

SourceCacheContext provides cache control settings matching NuGet.Client behavior.

func FromContext

func FromContext(ctx context.Context) *SourceCacheContext

FromContext retrieves the source cache context from the Go context. Returns nil if no cache context was set.

func NewSourceCacheContext

func NewSourceCacheContext() *SourceCacheContext

NewSourceCacheContext creates a new cache context with defaults.

func (*SourceCacheContext) Clone

func (ctx *SourceCacheContext) Clone() *SourceCacheContext

Clone creates a copy of the cache context.

type Stats

type Stats struct {
	Entries   int
	SizeBytes int64
}

Stats holds cache statistics.

Jump to

Keyboard shortcuts

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