r/learnjava • u/PresentationAway3815 • 27d ago
Built a small project called SwiftCache
I've been learning about caching systems and decided to build my own in-memory caching library in Java.
It supports different eviction strategies (LRU, LFU, FIFO, Random), TTL, read/write policies, and annotation-based caching.
This was primarily a learning project, and I'd really appreciate any feedback or suggestions on how I could improve it.
GitHub: https://github.com/saahilmahato/SwiftCache
Thanks for taking a look!
3
Upvotes
View all comments
2
u/Far-Bullfrog161 20d ago
Nice project idea, an in-memory cache is a great way to practice real API design. A few things I'd look at next if this were a PR at work: make capacity hard-bounded so every put that would exceed max size actually evicts, and document what happens on a race between get and expire. TTL is easy to get wrong under tests, so inject a Clock (or similar) instead of calling System.currentTimeMillis directly, then you can jump time in unit tests and assert eviction without sleeping. Be explicit about concurrency too: either document single-threaded use, or protect the map and the eviction structures together so you don't get a get that returns a stale entry while another thread is removing it. For the annotation side, keep the library core free of frameworks and put Spring or CDI integration in a separate module, otherwise people can't use the cache from a plain Java app. Lastly, write down the guarantees in the README in one place: eviction order per policy, whether values can be null, and whether compute-if-absent is atomic. That kind of clarity usually matters more than adding another policy.