Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0

README

Cache storage

Application composition uses process memory for response, model, and extraction caches, independent of the durable storage backend. The cache manager accepts a cache-owned interface. It cannot accept the application's durable KV handle directly. An injected store requires the explicit distributed strategy. The manager owns that store after successful construction.

The default Ristretto capacities are 256 MiB for responses, 16 MiB for models, and 16 MiB for extraction records. These capacities bound cache cost, not total process memory. Extraction caching retains its independent lifecycle when the operator disables response caching.

CSP12.1 must qualify shared-service configuration, bounded fills, and stream accumulation limits.

Shared response bytes use the dedicated cache service directly. No local refill can extend their server expiry. The package has no durable KV cache adapter, layered cache, or invalidation pub/sub implementation. Real-service tests cover shared expiry and isolation. Local tests cover expiry and clearing. Historical evidence retains the removed adapters' original defects and repairs.

No production qualification or latency improvement follows from this interface change alone.

Optional response fills

Response and model fills share two lifecycle-owned workers. The queue limits queued and active work to 1,024 entries and 4 MiB of charged bytes. The byte charge includes copied keys, copied payloads, and 96 bytes per job. It does not measure total process or transport memory. Admission drops optional work when capacity or the admission lock is unavailable. SetResponse does not confirm persistence or a subsequent cache hit.

Writes have a 500 ms context deadline. Cache-only adapters must honor cancellation for blocking work. Shutdown cancels fills and joins workers before closing the stores. The admin info response reports limits, retained work, completed fills, failures, and drops under response_cache. It exposes no cache keys or payloads.

Injected response-cache reads have a 2 ms deadline. Local response reads use no additional timer. Current authorization and catalog checks still govern cached delivery. Model invalidation advances a process-local epoch. Queued writes from earlier epochs cannot become visible after invalidation. Model serialization still runs on the caller before queue admission.

The encoder rejects strings and byte slices above 512 KiB and containers above 65,536 entries before encoding their contents. Encoded output cannot exceed the fill budget after key and job charges. Model fills transfer their encoded buffer and charge its capacity. Model, provider, and endpoint reads decode directly into the requested response type.

Extraction fills use an independent queue with the same bounds, even when the operator disables response caching. Together, both queues permit at most 2,048 queued or active entries, 8 MiB of charged data, and four workers. The admin extraction_cache field reports extraction pressure. response_cache reports the shared response and model queue.

Extraction serialization remains synchronous for eligible records. The document cache rejects inputs above 512 KiB before serialization. That input bound includes identity, text, and offering strings. Six-byte JSON escaping fits within the fill budget. Rejected inputs increment the extraction drop counter. Document delivery continues.

Aggregate heap, stream reconstruction, and full request latency still need qualification.

Stream retention

The canonical response-cache buffer limits each stream to 256 KiB of charged input and 1,024 events. Either limit discards all accumulated cache input and prevents further accumulation for that stream. Delivery continues with the original events. EOF, upstream failure, and close release retained input. The byte check precedes cloning and includes media, tool arguments, probability data, metadata, and strings. Copies own their strings so a short substring cannot retain a large provider buffer.

Fixed structure charges and doubled payload charges allow for slice growth and allocation rounding on supported 64-bit hosts. The charge is an admission bound. Full retained-heap and concurrent-stream qualification remain required. EOF response reconstruction and encoding remain synchronous and need separate latency qualification.

Enablement

The master cache switch controls every optional cache kind. Independent flags select chat, embeddings, models, providers, and extractions. See the operator controls. Semantic caching also requires chat caching and its existing opt-ins.

Dedicated shared response cache

The default STARPORT_CACHE_BACKEND=local uses process memory. Select STARPORT_CACHE_BACKEND=valkey with an explicit STARPORT_CACHE_URL and STARPORT_DEPLOYMENT_ID for shared response bytes. Model and extraction caches remain local. Use the same deployment ID for its replicas and different IDs for independent deployments. The default ID is local.

Keys use starport:v1:<base64url-deployment-id>:cache: with unpadded UTF-8 encoding. The admin cache status exposes the effective key_prefix for ACL provisioning.

The URI accepts credentials and an optional database number. Use valkeys:// or rediss:// for TLS with system trust and hostname verification. Plaintext is available on loopback. Remote plaintext requires STARPORT_CACHE_ALLOW_INSECURE=true. Query options, fragments, and additional endpoints are invalid. Redis URI syntax does not establish Redis compatibility qualification.

The cache service must be separate from durable KV. A different database or namespace does not isolate service eviction. Validation rejects the same declared host and port, including common loopback aliases. Operators must also check DNS aliases, proxies, and actual service ownership. The shared cache needs deployment-owned capacity and eviction settings.

Connection work runs in the background with bounded connection attempts and one-second recovery polling. Startup continues while the optional service is unavailable. Reads become misses through the existing proxy error path. Cache failure does not change permission or budget admission.

The connection owner retries initial failures. The client reconnects after connection loss. Shutdown cancels connection work and closes the dedicated client. The admin response_cache.shared status reports the configured service and its availability, without connection values.

Scratch development rejects shared-cache configuration. Use an initialized deployment to test the shared-service recipe. Native TLS deployment, aggregate transport memory, and cache-outage latency remain part of production acceptance.

Shared-cache trust and access

STARPORT_CACHE_CA_FILE selects an explicit PEM trust bundle for a TLS endpoint. The connection uses the bundle's roots. The loader accepts a regular file of at most 1 MiB. File access remains under deployment control. The file appears as cache-ca in the configuration file inventory.

Relative paths require STARPORT_RELATIVE_PATH_BASE=config and resolve under the selected configuration directory. The reader confines the final file lookup to its parent directory. Replace the bundle and restart Starport to change the active trust roots.

Provision a separate cache user for each deployment. Its ACL must restrict keys to the reported key_prefix followed by *. The cache needs these commands: hello, ping, select, client|setname, client|setinfo, get, strlen, set, eval, evalsha, and script|load. The service administrator owns ACL changes. Starport does not grant itself access or claim another namespace.

Replicas of one deployment can share the scoped credential. Independent deployments must use distinct credentials and namespaces.

The connection monitor writes and reads the reserved __starport_health_v1__ key under its namespace. The probe contains one fixed byte and expires after one second. It contains no caller data. A denied probe keeps the optional cache unavailable. It does not block required gateway operations.

The admin response_cache.shared.state field supplies a fixed diagnostic code.

State Recovery
connecting Wait for the bounded initial connection attempt.
ready The latest namespace probe succeeded.
unavailable Check the endpoint, network, and service.
tls_untrusted Configure the issuing CA and restart.
tls_hostname_mismatch Use the certificate's valid endpoint name or replace the certificate.
tls_certificate_invalid Check certificate dates and constraints, then replace invalid material.
authentication_failed Correct the cache credential and restart.
namespace_denied Correct the selected namespace or its server ACL.
closed The application closed the connection owner.

The TLS tests terminate TLS in a local relay before real Valkey traffic. They cover trusted, unknown, expired, and wrong-host certificates. Native service deployments still require CSP15 qualification.

Documentation

Overview

Package cache owns optional local and shared byte caches.

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheClosed = errors.New("cache is closed")

ErrCacheClosed reports a fill submitted after shutdown.

Functions

This section is empty.

Types

type BufferedLocalCache added in v1.3.0

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

BufferedLocalCache owns local bytes and a bounded optional fill queue.

func NewBufferedLocalCache added in v1.3.0

func NewBufferedLocalCache(sizeMB int64, ttl time.Duration) (*BufferedLocalCache, error)

NewBufferedLocalCache starts workers owned by the returned cache.

func (*BufferedLocalCache) Close added in v1.3.0

func (c *BufferedLocalCache) Close() error

Close joins workers before releasing their local store.

func (*BufferedLocalCache) FillStatus added in v1.3.0

func (c *BufferedLocalCache) FillStatus() FillStatus

FillStatus reports bounds and pressure without keys or payloads.

func (*BufferedLocalCache) Get added in v1.3.0

func (c *BufferedLocalCache) Get(ctx context.Context, key string) ([]byte, bool, error)

Get reads local bytes without waiting for outstanding fills.

func (*BufferedLocalCache) RecordDroppedFill added in v1.3.0

func (c *BufferedLocalCache) RecordDroppedFill()

RecordDroppedFill reports optional input rejected before encoding.

func (*BufferedLocalCache) Set added in v1.3.0

func (c *BufferedLocalCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set admits an optional fill; success does not guarantee a subsequent hit.

type FillStatus added in v1.3.0

type FillStatus struct {
	Shared          SharedStatus `json:"shared"`
	Enabled         bool         `json:"enabled"`
	EntryLimit      int64        `json:"entry_limit"`
	ByteLimit       int64        `json:"byte_limit"`
	WorkerLimit     int64        `json:"worker_limit"`
	RetainedEntries int64        `json:"retained_entries"`
	RetainedBytes   int64        `json:"retained_bytes"`
	ActiveFills     int64        `json:"active_fills"`
	DroppedFills    uint64       `json:"dropped_fills"`
	FailedFills     uint64       `json:"failed_fills"`
	CompletedFills  uint64       `json:"completed_fills"`
}

FillStatus reports optional response work without cache keys or payloads.

type LocalCache

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

LocalCache implements a local-only cache using Ristretto

func NewLocalCache

func NewLocalCache(sizeMB int64, ttl time.Duration) (*LocalCache, error)

NewLocalCache creates a new local-only cache

func (*LocalCache) Clear

func (l *LocalCache) Clear()

Clear removes all items from the cache

func (*LocalCache) Close

func (l *LocalCache) Close() error

Close closes the cache

func (*LocalCache) Get

func (l *LocalCache) Get(_ context.Context, key string) ([]byte, bool, error)

Get retrieves a value from the local cache

func (*LocalCache) Set

func (l *LocalCache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

Set stores a value in the local cache

func (*LocalCache) Stats

func (l *LocalCache) Stats() Stats

Stats returns cache statistics

type Manager

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

Manager manages different cache strategies for different data types

func NewCacheManager

func NewCacheManager(config ManagerConfig, store ResponseStore) (*Manager, error)

NewCacheManager uses bounded memory unless the caller supplies a cache-only store. The manager owns the supplied store after successful construction.

func (*Manager) Close

func (cm *Manager) Close() error

Close releases both cache stores.

func (*Manager) FillStatus added in v1.3.0

func (cm *Manager) FillStatus() FillStatus

FillStatus reads counters and limits from memory.

func (*Manager) GetModel

func (cm *Manager) GetModel(ctx context.Context, modelID string, target any) (bool, error)

GetModel retrieves model metadata (local cache only)

func (*Manager) GetResponse

func (cm *Manager) GetResponse(ctx context.Context, key string) ([]byte, bool, error)

GetResponse retrieves a cached LLM response

func (*Manager) InvalidateModels

func (cm *Manager) InvalidateModels()

InvalidateModels clears all model metadata from local cache

func (*Manager) SetModel

func (cm *Manager) SetModel(ctx context.Context, modelID string, model any) error

SetModel caches model metadata (local cache only)

func (*Manager) SetResponse

func (cm *Manager) SetResponse(ctx context.Context, key string, response []byte) error

SetResponse admits an optional fill without waiting for persistence.

func (*Manager) Stats

func (cm *Manager) Stats() map[string]Stats

Stats returns aggregated cache statistics.

type ManagerConfig

type ManagerConfig struct {
	// LLM Responses configuration
	Responses struct {
		Strategy      string        `env:"STRATEGY,default=auto"`
		TTL           time.Duration `env:"TTL,default=1h"`
		MaxItemSizeKB int           `env:"MAX_ITEM_SIZE_KB,default=1024"`
		LocalSizeMB   int64         `env:"LOCAL_SIZE_MB,default=256"`
	} `env:",prefix=RESPONSES_"`

	// Model Metadata configuration
	Models struct {
		Strategy string        `env:"STRATEGY,default=local"`
		TTL      time.Duration `env:"TTL,default=6h"`
		SizeMB   int64         `env:"SIZE_MB,default=16"`
	} `env:",prefix=MODELS_"`
}

ManagerConfig configures the cache manager

type ResponseStore added in v1.3.0

type ResponseStore interface {
	Get(context.Context, string) ([]byte, bool, error)
	Set(context.Context, string, []byte, time.Duration) error
	Stats() Stats
	Close() error
}

ResponseStore owns optional response bytes and their lifecycle. Blocking operations must honor context cancellation.

type SharedConfig added in v1.3.0

type SharedConfig struct {
	URL           string
	DeploymentID  string
	AllowInsecure bool
	CAFile        string
}

SharedConfig selects one cache endpoint and its trust roots.

type SharedStatus added in v1.3.0

type SharedStatus struct {
	Configured bool   `json:"configured"`
	Available  bool   `json:"available"`
	State      string `json:"state"`
	KeyPrefix  string `json:"key_prefix"`
}

SharedStatus reports connection health without endpoint or credential values.

type SharedStore added in v1.3.0

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

SharedStore owns an optional connection independently of authoritative storage.

func OpenShared added in v1.3.0

func OpenShared(config SharedConfig) (*SharedStore, error)

OpenShared starts bounded connection work without delaying application startup.

func (*SharedStore) Close added in v1.3.0

func (s *SharedStore) Close() error

Close cancels connection attempts and joins the connection owner.

func (*SharedStore) Get added in v1.3.0

func (s *SharedStore) Get(ctx context.Context, key string) ([]byte, bool, error)

Get bounds a response before the server transfers its payload.

func (*SharedStore) Set added in v1.3.0

func (s *SharedStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set writes only finite, bounded cache entries.

func (*SharedStore) SharedStatus added in v1.3.0

func (s *SharedStore) SharedStatus() SharedStatus

SharedStatus reads local connection evidence.

func (*SharedStore) Stats added in v1.3.0

func (s *SharedStore) Stats() Stats

Stats excludes service identifiers and credentials.

type Stats

type Stats struct {
	RetainedEntries int64  `json:"retained_entries,omitzero"`
	RetainedBytes   int64  `json:"retained_bytes,omitzero"`
	ActiveFills     int64  `json:"active_fills,omitzero"`
	DroppedFills    uint64 `json:"dropped_fills,omitzero"`
	FailedFills     uint64 `json:"failed_fills,omitzero"`
	CompletedFills  uint64 `json:"completed_fills,omitzero"`

	// Hits is the number of cache hits
	Hits uint64 `json:"hits"`
	// Misses is the number of cache misses
	Misses uint64 `json:"misses"`
	// Sets is the number of items added to cache
	Sets uint64 `json:"sets"`
	// Deletes is the number of items deleted from cache
	Deletes uint64 `json:"deletes"`
	// Evictions is the number of items evicted due to size/TTL
	Evictions uint64 `json:"evictions"`
	// HitRate is the cache hit rate (hits / (hits + misses))
	HitRate float64 `json:"hit_rate"`
	// Size is the current number of items in cache
	Size int64 `json:"size"`
	// SizeInBytes is the approximate memory usage
	SizeInBytes int64 `json:"size_in_bytes"`
}

Stats contains cache performance metrics.

Jump to

Keyboard shortcuts

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