A question about expiring map #1775
Answered
by
ben-manes
WeeeeeeeeeeS
asked this question in
Q&A
-
I use Caffeine cache in the following way: Cache<String, GuildInfo> guilds = Caffeine.newBuilder()
.expireAfterAccess(45, TimeUnit.MINUTES)
.build(); How can I configure the cache so that certain entries do not expire after 45 minutes? I want some"`GuildInfo" objects to remain in the cache for a longer period (Without creating another cache object) |
Beta Was this translation helpful? Give feedback.
Answered by
ben-manes
Sep 7, 2024
Replies: 1 comment
-
In that case you would use a custom expiration policy. The Cache<String, GuildInfo> guilds = Caffeine.newBuilder()
.expireAfter(new ExpiryAfterAccess<String, GuildInfo> {
@Override public long expireAfterCreate(String key, GuildInfo guild, long currentTime) {
return guildDuration(guild);
}
@Override public long expireAfterUpdate(String key, GuildInfo guild,
long currentTime, long currentDuration) {
return guildDuration(guild);
}
@Override public long expireAfterRead(String key, GuildInfo guild,
long currentTime, long currentDuration) {
return guildDuration(guild);
}
private long guildDuration(GuildInfo) {
return (guild.size() < 100)
? TimeUnit.MINUTES.toNanos(45)
: TimeUnit.MINUTES.toNanos(90)
}
}).build(); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ben-manes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In that case you would use a custom expiration policy. The
expireAfterAccess
means it is set on creation, insert, update, or read of the entry. Here is an example where larger guilds are given more idle time before expiration.