Semantic caching library for Go. Stores values with embedding vectors, retrieves by similarity. Sync-only Cache[K, V] type with pluggable backends and embedding providers.
semanticcache/
cache.go, errors.go Cache type, constructors, sentinel errors
types/ Backend[K,V] and EmbeddingProvider interfaces
options/ Functional options (With* functions), config errors
backends/
inmemory/ LRU, LFU, FIFO (thread-safe)
remote/ Redis (JSON storage)
providers/
openai/ OpenAI embeddings (official SDK)
local/ Hash-based provider for testing (no API key)
similarity/ Cosine, Euclidean, DotProduct, Manhattan, Pearson
chunker/ Text chunking utilities
tokenizer/ Token counting (OpenAI, Anthropic, Gemini)
- Sync-only. No async cache.
Backend[K, V]has 9 methods. Every backend implements all of them.- No centralized errors package. Each package defines its own errors.
- Functional options pattern for configuration.
context.Contexton all operations.- Generics throughout:
Cache[K comparable, V any].
go test ./... # all tests
go test -race ./... # with race detector
go test -bench=. ./... # benchmarks
go vet ./... # static analysis
gofmt -l . # formatting check
go build ./... # build all
import "github.com/botirk38/semanticcache"All subpackages: github.com/botirk38/semanticcache/{options,types,backends/inmemory,...}
- Implement
types.Backend[K, V](9 methods) - Add
With*Backendoption inoptions/options.go - Add compile-time check:
var _ types.Backend[string, string] = (*YourBackend[string, string])(nil) - Add tests in the backend's package
- Add a re-export in
backends/backends.go
- Implement
types.EmbeddingProvider(2 methods:EmbedText,Close) - Optionally implement
types.BatchEmbeddingProvider - Add
With*Provideroption inoptions/options.go - Add tests (use
httptestfor HTTP providers) - Add a re-export in
providers/providers.go
- Create a new file in
similarity/ - Implement
func(a, b []float64) float64 - Add tests in
similarity_test.go
- Each package defines its own sentinel errors.
- Root package:
ErrClosed,ErrZeroKey,ErrInvalidN - Options:
ErrNilBackend,ErrNilProvider,ErrNilComparator - Chunker:
ErrInvalidChunkSize,ErrEmptyText, etc.