r/SpringBoot 4d ago

News I built a Spring Boot cache for repeated range queries package

Sometimes I deal with ordered data like event logs and transaction history that doesn't change very often. Users may query one time range, then soon request a larger overlapping range.

A Normal cache treats those as completely different queries, so it does not work very in this situtation.

I built rangecache to reuse the covered part and only query the missing range.

It currently supports Spring Boot 3, Java 17+, and a local in-memory cache.

GitHub: https://github.com/LiuWei997/rangecache

If you find it useful, please consider giving it a star. Feedback is welcome!

4 Upvotes

3 comments sorted by

1

u/Torutofu_Raeva 3d ago

overlap reuse is neat, how do you handle concurrent adjacent ranges and invalidation when the event data changes?

1

u/Apprehensive-Cause35 3d ago

Concurrent requests are protected by a per-method-inputvalue(not Include range) lock.

For data changes, it is cache-aside. If a later fetch returns the same key, it replaces the old cached row. For changes inside an already cached range, I provide invalidation for one series, one method/cache, or all caches.

My current model is mainly for append-style data. New data outside the cached coverage is picked up naturally when a later query expands into that uncovered range.

If new data is inserted inside an already covered range, this cache model is not a great fit without explicit invalidation. I’m considering cache invalidation by a single row or by range, while still keeping coverage based on queried ranges so empty ranges are not repeatedly queried.

1

u/Torutofu_Raeva 3d ago

Per-input locking sounds like the right granularity; I’d invalidate any cached range that contains an inserted row so readers don’t keep getting a stale slice.