Jane Hayes
Jane Hayes

Reputation: 115

How to set a Caffeine cache to not expire

I have a simple spring boot app and I'm using Caffeine for caching. I'm configuring it using spring boot properties. How do I sent it to not expire?

Upvotes: 0

Views: 3012

Answers (1)

Jaskaran Singh
Jaskaran Singh

Reputation: 139

How about you just set it to sufficiently high value just like they are doing here. https://github.com/ben-manes/caffeine/blob/e8ff6d3261e7f5666d2b486352cc04b2874d70ed/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Async.java

You can implement your own expiry like this.

Caffeine
            .newBuilder()
            .expireAfter(new Expiry<String, String>() {
                @Override
                public long expireAfterCreate(@Nonnull String key, @Nonnull String value,
                                              long currentTime) {
                    //return Duration.ofDays(1).toNanos();
                    return   Long.MAX_VALUE;
                }

                @Override
                public long expireAfterUpdate(@Nonnull String key, @Nonnull String value, long currentTime,
                                              long currentDuration) {
                    return currentDuration;
                }

                @Override
                public long expireAfterRead(@Nonnull String key, @Nonnull String value, long currentTime,
                                            long currentDuration) {
                    return currentDuration;
                }
            }).build()

Upvotes: 1

Related Questions