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 ¶
- Variables
- func CacheKey(prefix string, pattern *DetectedPattern) string
- func CacheKeyForChunks(prefix string, chunks []types.Chunk) string
- func CacheKeyForQuery(prefix, query string, topK int) string
- func CacheKeyForText(prefix, text string) string
- func HashChunks(chunks []types.Chunk) string
- func HashText(text string) string
- type Cache
- type Config
- type DetectedPattern
- type Entry
- type MemoryCache
- func (c *MemoryCache) Clear(ctx context.Context) error
- func (c *MemoryCache) Close() error
- func (c *MemoryCache) Delete(ctx context.Context, key string) error
- func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *MemoryCache) Has(ctx context.Context, key string) bool
- func (c *MemoryCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *MemoryCache) Stats() Stats
- type PatternDetector
- type PatternType
- type RedisCache
- func (c *RedisCache) Clear(ctx context.Context) error
- func (c *RedisCache) Close() error
- func (c *RedisCache) Delete(ctx context.Context, key string) error
- func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetTTL(ttl time.Duration) time.Duration
- func (c *RedisCache) Has(ctx context.Context, key string) bool
- func (c *RedisCache) PrefixKey(key string) string
- func (c *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *RedisCache) Stats() Stats
- type RedisConfig
- type Stats
Constants ¶
This section is empty.
Variables ¶
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 ¶
CacheKeyForChunks generates a cache key for a chunk set.
func CacheKeyForQuery ¶
CacheKeyForQuery generates a cache key for a query.
func CacheKeyForText ¶
CacheKeyForText generates a cache key for raw text.
func HashChunks ¶
HashChunks creates a combined hash for a set of chunks.
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.
type DetectedPattern ¶
type DetectedPattern struct {
Type PatternType
Hash string
Text string
Metadata map[string]string
}
DetectedPattern represents a detected cacheable pattern.
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) Has ¶
func (c *MemoryCache) Has(ctx context.Context, key string) bool
Has checks if a key exists.
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) 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.
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.