SQLite disk cache for embeddings keyed by content hash, model id and dimension, with batched misses, bit-exact float64 storage and LRU eviction
Embedding the same text again on every run of an indexing job or test suite costs time and, with
a hosted model, money. A common workaround is an ad hoc dictionary or pickle file keyed by the
raw text, which is lost between processes, ignores which model produced the vector and stores
the source text on disk. This package wraps any embedder that exposes model_id, dimension
and embed(texts) with a SQLite cache keyed by a SHA-256 hash of the model id, the dimension and
the text, so only cache misses reach the embedder and the text itself is never written to the
file. It validates every returned vector, evicts least-recently-accessed entries beyond
max_entries, and is meant for one process at a time; it does not call any embedding service
itself.
Not published to PyPI yet. Install it from GitHub:
pip install "git+https://github.com/Trysac/u020-embedding-cache"
# or
uv add "git+https://github.com/Trysac/u020-embedding-cache"Requires Python 3.10 or newer. No runtime dependencies.
from embedding_cache import EmbeddingCache
class CountingEmbedder:
"""Stand-in for a real embedding model; records how many texts it was sent."""
model_id = "toy-model"
dimension = 2
def __init__(self):
self.calls = []
def embed(self, texts):
self.calls.append(len(texts))
return [[float(len(text)), 1.0] for text in texts]
embedder = CountingEmbedder()
with EmbeddingCache(":memory:", embedder) as cache: # or a file path such as "cache.db"
# "hello " and "hello" share a key: text is NFC-normalized and right-stripped.
vectors = cache.embed(["hello", "world", "hello "])
print(len(vectors)) # 3
# The embedder received two unique texts in one batch.
print(embedder.calls[-1]) # 2
# "hello" is served from the cache; only "new text" reaches the embedder.
cache.embed(["hello", "new text"])
print(embedder.calls[-1]) # 1
print(cache.get("hello")[0]) # 5.0
print(cache.get("never embedded")) # None
stats = cache.stats()
print(stats.hits) # 2
print(stats.misses) # 4
print(stats.entries) # 3- Architecture and decisions
- Testing and security
- Project record and stack
- Changelog and contributing guide
MIT (c) 2026 Miguel Gomez