0

I create cache for manual storing values:

private Cache<Long, SmsData> codeCache = Caffeine.newBuilder()
        .expireAfterWrite(24, TimeUnit.HOURS)
        .weakKeys()
        .weakValues()
        .build();

In public method after some calcultion I try to store value:

SmsData data = SopdSmsData();
data.setSendCount(++currentSendCount);
data.setCheckCount(0);
codeCache.put(id, data);

After that I want to get value, but its null:

SmsData data = codeCache.getIfPresent(id);

What I missed? Thanks in advance.

Khilarian
  • 249
  • 1
  • 9
  • weakKeys are identity based (==, same reference), so an auto-boxed long wouldn’t map to the same entry. And weakValues means it will evict if no strong references exist. You can use a listener to log evictions to debug with as you familiarize yourself with the cache. – Ben Manes Nov 30 '22 at 13:42

1 Answers1

0

I solve it next way: make field static final and remove weakKeya(), weakValues()

Khilarian
  • 249
  • 1
  • 9