r/learnjava • • 16d 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!

4 Upvotes

4 comments sorted by

•

u/AutoModerator 16d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Far-Bullfrog161 10d 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.

1

u/PresentationAway3815 9d ago

Thanks for the feedback and insights. I will study inner workings of an in-memory cache more and implement your suggestions.