cache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cache provides KV caching for repeated context patterns. It enables sub-millisecond retrieval for common prompts, tool definitions, and other frequently accessed content.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = errors.New("key not found")
	ErrKeyTooLarge   = errors.New("key exceeds maximum size")
	ErrValueTooLarge = errors.New("value exceeds maximum size")
	ErrCacheFull     = errors.New("cache is full")
)

Common errors.

Functions

func CacheKey

func CacheKey(prefix string, pattern *DetectedPattern) string

CacheKey generates a cache key for a pattern.

func CacheKeyForChunks

func CacheKeyForChunks(prefix string, chunks []types.Chunk) string

CacheKeyForChunks generates a cache key for a chunk set.

func CacheKeyForQuery

func CacheKeyForQuery(prefix, query string, topK int) string

CacheKeyForQuery generates a cache key for a query.

func CacheKeyForText

func CacheKeyForText(prefix, text string) string

CacheKeyForText generates a cache key for raw text.

func HashChunks

func HashChunks(chunks []types.Chunk) string

HashChunks creates a combined hash for a set of chunks.

func HashText

func HashText(text string) string

HashText creates a SHA-256 hash of the text.

Types

type Cache

type Cache interface {
	// Get retrieves a value by key. Returns ErrNotFound if not present.
	Get(ctx context.Context, key string) ([]byte, error)

	// Set stores a value with optional TTL. Zero TTL means no expiration.
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

	// Delete removes a key from the cache.
	Delete(ctx context.Context, key string) error

	// Has checks if a key exists without retrieving the value.
	Has(ctx context.Context, key string) bool

	// Clear removes all entries from the cache.
	Clear(ctx context.Context) error

	// Stats returns cache statistics.
	Stats() Stats

	// Close releases resources.
	Close() error
}

Cache defines the interface for KV caching.

type Config

type Config struct {
	// MaxSize is the maximum number of entries (0 = unlimited).
	MaxSize int64

	// MaxSizeBytes is the maximum memory in bytes (0 = unlimited).
	MaxSizeBytes int64

	// DefaultTTL is the default expiration time for entries without explicit TTL.
	DefaultTTL time.Duration

	// CleanupInterval is how often to run expiration cleanup.
	CleanupInterval time.Duration
}

Config holds cache configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type DetectedPattern

type DetectedPattern struct {
	Type     PatternType
	Hash     string
	Text     string
	Metadata map[string]string
}

DetectedPattern represents a detected cacheable pattern.

type Entry

type Entry struct {
	Key       string
	Value     []byte
	CreatedAt time.Time
	ExpiresAt time.Time
	Size      int64
}

Entry represents a cached item.

func (Entry) IsExpired

func (e Entry) IsExpired() bool

IsExpired checks if the entry has expired.

type MemoryCache

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

MemoryCache is an in-memory LRU cache with TTL support.

func NewMemoryCache

func NewMemoryCache(cfg Config) *MemoryCache

NewMemoryCache creates a new in-memory LRU cache.

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(ctx context.Context) error

Clear removes all entries.

func (*MemoryCache) Close

func (c *MemoryCache) Close() error

Close stops the cleanup goroutine and releases resources.

func (*MemoryCache) Delete

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

Delete removes a key from the cache.

func (*MemoryCache) Get

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

Get retrieves a value by key.

func (*MemoryCache) Has

func (c *MemoryCache) Has(ctx context.Context, key string) bool

Has checks if a key exists.

func (*MemoryCache) Set

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

Set stores a value with optional TTL.

func (*MemoryCache) Stats

func (c *MemoryCache) Stats() Stats

Stats returns cache statistics.

type PatternDetector

type PatternDetector struct {
	// MinLength is the minimum text length to consider for pattern detection.
	MinLength int

	// SystemPromptPrefixes are common prefixes that indicate system prompts.
	SystemPromptPrefixes []string

	// ToolDefinitionMarkers are strings that indicate tool definitions.
	ToolDefinitionMarkers []string
}

PatternDetector identifies and hashes common context patterns.

func NewPatternDetector

func NewPatternDetector() *PatternDetector

NewPatternDetector creates a pattern detector with defaults.

func (*PatternDetector) DetectChunkPatterns

func (d *PatternDetector) DetectChunkPatterns(chunks []types.Chunk) []DetectedPattern

DetectChunkPatterns analyzes chunks and returns cacheable patterns.

func (*PatternDetector) DetectPattern

func (d *PatternDetector) DetectPattern(text string) *DetectedPattern

DetectPattern analyzes text and returns pattern information.

type PatternType

type PatternType string

PatternType identifies the type of detected pattern.

const (
	PatternTypeUnknown  PatternType = "unknown"
	PatternTypeSystem   PatternType = "system_prompt"
	PatternTypeTool     PatternType = "tool_definition"
	PatternTypeCode     PatternType = "code_block"
	PatternTypeDocument PatternType = "document"
)

type RedisCache

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

RedisCache implements Cache using Redis as the backend. This is a stub implementation - actual Redis client integration would require adding a Redis client dependency (e.g., go-redis).

func NewRedisCache

func NewRedisCache(cfg RedisConfig) (*RedisCache, error)

NewRedisCache creates a new Redis-backed cache. Note: This is a placeholder. Full implementation requires a Redis client.

func (*RedisCache) Clear

func (c *RedisCache) Clear(ctx context.Context) error

Clear removes all entries with the configured prefix.

func (*RedisCache) Close

func (c *RedisCache) Close() error

Close releases the Redis connection pool.

func (*RedisCache) Delete

func (c *RedisCache) Delete(ctx context.Context, key string) error

Delete removes a key from the cache.

func (*RedisCache) Get

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

Get retrieves a value by key.

func (*RedisCache) GetTTL

func (c *RedisCache) GetTTL(ttl time.Duration) time.Duration

GetTTL returns the TTL to use, falling back to default.

func (*RedisCache) Has

func (c *RedisCache) Has(ctx context.Context, key string) bool

Has checks if a key exists.

func (*RedisCache) PrefixKey

func (c *RedisCache) PrefixKey(key string) string

PrefixKey adds the configured prefix to a key.

func (*RedisCache) Set

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

Set stores a value with optional TTL.

func (*RedisCache) Stats

func (c *RedisCache) Stats() Stats

Stats returns cache statistics.

type RedisConfig

type RedisConfig struct {
	// URL is the Redis connection URL (e.g., redis://localhost:6379).
	URL string

	// Password for Redis authentication.
	Password string

	// DB is the Redis database number.
	DB int

	// KeyPrefix is prepended to all keys.
	KeyPrefix string

	// DefaultTTL is the default expiration for keys.
	DefaultTTL time.Duration

	// PoolSize is the connection pool size.
	PoolSize int

	// DialTimeout is the connection timeout.
	DialTimeout time.Duration

	// ReadTimeout is the read operation timeout.
	ReadTimeout time.Duration

	// WriteTimeout is the write operation timeout.
	WriteTimeout time.Duration
}

RedisConfig holds Redis connection configuration.

func DefaultRedisConfig

func DefaultRedisConfig() RedisConfig

DefaultRedisConfig returns sensible defaults.

type Stats

type Stats struct {
	// Hits is the number of successful cache retrievals.
	Hits int64

	// Misses is the number of cache misses.
	Misses int64

	// Sets is the number of cache writes.
	Sets int64

	// Deletes is the number of cache deletions.
	Deletes int64

	// Evictions is the number of entries evicted due to size limits.
	Evictions int64

	// Expirations is the number of entries expired due to TTL.
	Expirations int64

	// Size is the current number of entries.
	Size int64

	// SizeBytes is the current memory usage in bytes.
	SizeBytes int64

	// MaxSize is the maximum number of entries allowed.
	MaxSize int64

	// MaxSizeBytes is the maximum memory allowed in bytes.
	MaxSizeBytes int64
}

Stats holds cache performance metrics.

func (Stats) HitRate

func (s Stats) HitRate() float64

HitRate returns the cache hit rate as a percentage.

Jump to

Keyboard shortcuts

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