cache

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides a size-bounded in-memory LRU cache for static files. It wraps github.com/hashicorp/golang-lru/v2 and tracks total byte usage to enforce a maximum memory ceiling independent of item count.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a thread-safe, size-bounded LRU cache for CachedFile entries.

func NewCache

func NewCache(maxBytes int64) *Cache

NewCache creates a new Cache with the given maximum byte capacity. If maxBytes is <= 0, a default of 256 MB is used.

func (*Cache) Flush

func (c *Cache) Flush()

Flush removes all entries from the cache.

func (*Cache) Get

func (c *Cache) Get(path string) (*CachedFile, bool)

Get returns the cached file for the given path key, or (nil, false) on miss.

func (*Cache) Put

func (c *Cache) Put(path string, f *CachedFile)

Put stores a file in the cache under the given path key. If adding the new entry would exceed maxBytes, it evicts LRU entries until enough space is available. If the single entry is larger than maxBytes, it is not cached.

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns a snapshot of current cache statistics.

type CacheStats

type CacheStats struct {
	// Hits is the total number of successful cache lookups.
	Hits int64
	// Misses is the total number of failed cache lookups.
	Misses int64
	// CurrentBytes is the current total byte usage.
	CurrentBytes int64
	// EntryCount is the current number of entries.
	EntryCount int
}

CacheStats holds runtime statistics for the cache.

type CachedFile

type CachedFile struct {
	// Data is the raw (uncompressed) file content.
	Data []byte
	// GzipData is the pre-compressed gzip content, or nil if unavailable.
	GzipData []byte
	// BrData is the pre-compressed brotli content, or nil if unavailable.
	BrData []byte
	// ETag is the first 16 hex characters of sha256(Data), without quotes.
	ETag string
	// ETagFull is the pre-formatted weak ETag ready for use in HTTP headers,
	// e.g. `W/"abc123"`. Pre-computing it avoids a per-request string alloc.
	ETagFull string
	// LastModified is the file's modification time.
	LastModified time.Time
	// ContentType is the detected MIME type.
	ContentType string
	// Size is the length of Data in bytes.
	Size int64
}

CachedFile holds the content and metadata for a single cached file.

Jump to

Keyboard shortcuts

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