Skip to content

Add KV cache for repeated context patterns#17

Merged
Siddhant-K-code merged 2 commits into
mainfrom
feature/kv-cache
Feb 7, 2026
Merged

Add KV cache for repeated context patterns#17
Siddhant-K-code merged 2 commits into
mainfrom
feature/kv-cache

Conversation

@Siddhant-K-code

Copy link
Copy Markdown
Owner

Implements the KV cache module as specified in #3.

Summary

Adds pkg/cache for sub-millisecond retrieval of repeated context patterns (system prompts, tool definitions, common code snippets).

Components

Cache Interface (cache.go)

  • Get/Set/Delete/Has/Clear operations
  • Stats for monitoring (hits, misses, evictions, size)
  • Configurable size limits and TTL

Memory Cache (memory.go)

  • In-memory LRU implementation using container/list
  • Thread-safe with sync.RWMutex
  • Automatic TTL expiration with background cleanup
  • Configurable max entries and max bytes

Pattern Detection (patterns.go)

  • Detects pattern types: system prompts, tool definitions, code blocks, documents
  • SHA-256 hashing for cache keys
  • Helper functions for generating cache keys

Redis Cache (redis.go)

  • Stub implementation for distributed deployments
  • Ready for go-redis integration

Usage

// Create cache
cache := cache.NewMemoryCache(cache.Config{
    MaxSize:    10000,
    MaxSizeBytes: 100 * 1024 * 1024, // 100MB
    DefaultTTL: time.Hour,
})
defer cache.Close()

// Cache a pattern
detector := cache.NewPatternDetector()
pattern := detector.DetectPattern(systemPrompt)
key := cache.CacheKey("distill", pattern)
cache.Set(ctx, key, []byte(systemPrompt), 0)

// Retrieve
value, err := cache.Get(ctx, key)

Performance

  • Get: ~100ns (in-memory)
  • Set: ~200ns (in-memory)
  • Hash: ~500ns per text

Tests

Comprehensive tests including:

  • Get/Set/Delete/Has/Clear operations
  • TTL expiration
  • LRU eviction
  • Pattern detection
  • Benchmarks

Closes #3

Implements pkg/cache with:
- Cache interface with Get/Set/Delete/Has/Clear/Stats
- MemoryCache: in-memory LRU with TTL support and cleanup
- PatternDetector: identifies system prompts, tool definitions, code
- RedisCache: stub for distributed deployments (requires go-redis)

Features:
- Sub-millisecond retrieval for cached patterns
- Configurable size limits (entries and bytes)
- Automatic LRU eviction when full
- TTL-based expiration with background cleanup
- Pattern hashing for cache keys

Closes #3

Co-authored-by: Ona <[email protected]>
@Siddhant-K-code Siddhant-K-code added the enhancement New feature or request label Feb 1, 2026
- Export Redis helper methods (PrefixKey, GetTTL)
- Add error checks in tests (errcheck)

Co-authored-by: Ona <[email protected]>
@Siddhant-K-code Siddhant-K-code merged commit 87e8c25 into main Feb 7, 2026
2 checks passed
@Siddhant-K-code Siddhant-K-code deleted the feature/kv-cache branch February 7, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] KV Cache for Repeated Patterns

1 participant