Semantic caching library for Go. Sync-only Cache[K, V] with pluggable backends and embedding providers.
go test ./... # all tests
go test -race ./... # race detector
go test -bench=. ./... # benchmarks
go test -cover ./... # coverage
go vet ./... # static analysis
gofmt -l . # formatting check
go build ./... # build all
import "github.com/botirk38/semanticcache"Subpackages: options, types, backends/inmemory, backends/remote, providers/openai, providers/local, similarity, chunker, tokenizer.
cache.go+errors.go--Cache[K, V]type, constructors, sentinel errors (ErrClosed,ErrZeroKey,ErrInvalidN)types/--Backend[K, V]interface (9 methods),EmbeddingProvider,BatchEmbeddingProvideroptions/-- functional options (With*functions), config errors (ErrNilBackend,ErrNilProvider,ErrNilComparator)backends/inmemory/-- LRU, LFU, FIFO (thread-safe viasync.RWMutex)backends/remote/-- Redis (JSON storage, requires RedisJSON or Redis 7.2+)providers/openai/-- OpenAI SDK, default modeltext-embedding-3-smallproviders/local/-- hash-based provider for testing (no API key, not semantically meaningful)similarity/--func(a, b []float64) float64functions (cosine, euclidean, dot, manhattan, pearson)chunker/-- text chunking with configurable strategy, its own errorstokenizer/-- token counting for OpenAI (local), Anthropic (API), Gemini (API)
Each package defines its own errors. No centralized errors package.
- Implement all 9 methods of
types.Backend[K, V] - Add
With*Backendoption inoptions/options.go - Add compile-time check:
var _ types.Backend[string, string] = (*YourBackend[string, string])(nil) - Add tests + benchmarks
- Add re-export in
backends/backends.go - Add
README.mdandAGENTS.mdin the new package
- Implement
types.EmbeddingProvider(EmbedText,Close) - Optionally implement
types.BatchEmbeddingProvider - Add
With*Provideroption inoptions/options.go - Add tests (use
httptestfor HTTP-based providers) - Add re-export in
providers/providers.go - Add
README.mdandAGENTS.mdin the new package
- New file in
similarity/, signaturefunc(a, b []float64) float64 - Return 0 for mismatched lengths or empty vectors
- Add tests in
similarity_test.go
- Standard Go conventions.
gofmtenforced in CI. - Functional options pattern for configuration.
context.Contextfirst argument on all I/O methods.- Generics:
[K comparable, V any]throughout. - Each module has its own
README.md(for humans) andAGENTS.md(for agents).