Add an opt-in caching decorator for EmbeddingModel
Author: AshfaqbsCreated Sep 26, 2026Updated Sep 26, 2026
Labelsstatus: waiting-for-triage
Expected Behavior
An opt-in EmbeddingModel decorator that caches embeddings by input text, for example CachingEmbeddingModel(EmbeddingModel delegate, Cache cache) using Spring's Cache abstraction so users pick Caffeine, Redis and so on.
Why it matters: embedding calls are billed per token and are frequently repeated with identical input.
- Re-running an ETL pipeline over mostly unchanged documents re-embeds every chunk.
- RAG query embedding repeats for popular questions.
TokenCountBatchingStrategybatches by size, but nothing avoids embedding text that was already embedded.
Design sketch:
- Key = model name + dimensions + text (so changing model or
EmbeddingOptionsnever returns a stale vector). embed(List<String>)looks up each text, sends only the misses to the delegate in one batch, then merges results in the original order.call(EmbeddingRequest)behaves the same, and options are part of the key.- Not a default; wired only when the user wraps a model or sets a property.
Current Behavior
I searched the repo for an embedding cache and found none. spring-ai-redis-semantic-cache caches chat responses, not embeddings, and I found no open or closed issue asking for this.
I'd like maintainer feedback on scope (core vs. a separate module) before opening a PR. I'm happy to implement it with unit tests.
Source: spring-projects/spring-ai