Documentation
¶
Overview ¶
Package cache owns optional local and shared byte caches.
Index ¶
- Variables
- type BufferedLocalCache
- func (c *BufferedLocalCache) Close() error
- func (c *BufferedLocalCache) FillStatus() FillStatus
- func (c *BufferedLocalCache) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (c *BufferedLocalCache) RecordDroppedFill()
- func (c *BufferedLocalCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type FillStatus
- type LocalCache
- type Manager
- func (cm *Manager) Close() error
- func (cm *Manager) FillStatus() FillStatus
- func (cm *Manager) GetModel(ctx context.Context, modelID string, target any) (bool, error)
- func (cm *Manager) GetResponse(ctx context.Context, key string) ([]byte, bool, error)
- func (cm *Manager) InvalidateModels()
- func (cm *Manager) SetModel(ctx context.Context, modelID string, model any) error
- func (cm *Manager) SetResponse(ctx context.Context, key string, response []byte) error
- func (cm *Manager) Stats() map[string]Stats
- type ManagerConfig
- type ResponseStore
- type SharedConfig
- type SharedStatus
- type SharedStore
- func (s *SharedStore) Close() error
- func (s *SharedStore) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (s *SharedStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (s *SharedStore) SharedStatus() SharedStatus
- func (s *SharedStore) Stats() Stats
- type Stats
Constants ¶
This section is empty.
Variables ¶
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
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.
type FillStatus ¶ added in v1.3.0
type FillStatus struct {
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
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) FillStatus ¶ added in v1.3.0
func (cm *Manager) FillStatus() FillStatus
FillStatus reads counters and limits from memory.
func (*Manager) GetResponse ¶
GetResponse retrieves a cached LLM response
func (*Manager) InvalidateModels ¶
func (cm *Manager) InvalidateModels()
InvalidateModels clears all model metadata from local cache
func (*Manager) SetResponse ¶
SetResponse admits an optional fill without waiting for persistence.
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 {
}
SharedConfig selects one cache endpoint and its trust roots.
type SharedStatus ¶ added in v1.3.0
type SharedStatus struct {
}
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
Get bounds a response before the server transfers its payload.
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.