1

We are using Caffeine for caching purpose. The setup seems to be pretty simple. We setup the cache in the following way

LoadingCache<Triple<Long, Long, Long>, Result> cache = Caffeine.newBuilder()
            .maximumSize(1000)
            .refreshAfterWrite(240, TimeUnit.MINUTES)
            .build(new CacheDataLoader());

public class CacheDataLoader implements CacheLoader<Triple<Long, Long, Long>, Result>  {
    @Override
    public Result load(@Nonnull Triple<Long, Long, Long> id) throws Exception 
    {
      -------
    }

    @Override
    public Result reload(@Nonnull Triple<Long, Long, Long> id, @Nonnull Result oldValue) throws Exception {
       ---------
    }
}

When we do cache.get(id), it is always triggering the load function in CacheDataLoader which will load from a database. As a result the data is never being fetched from in memory. Based on the documentation, the load should be triggered only if the requested key is not in memory. Is that not correct? Is there anything wrong with how we are configuring the cache.

Any insight is appreciated.

Thanks.

lakshmi
  • 385
  • 3
  • 8
  • 18
  • This is often because the cache key lacks good equals and hashCode methods. Can you check those? – Ben Manes May 19 '17 at 20:51
  • @BenManes thanks for your response. That was my hunch initially since we are using Triple so I changed it to just String and still had the same behaviour – lakshmi May 22 '17 at 14:54
  • Can you provide a test case? – Ben Manes May 22 '17 at 15:01
  • The other possibilities are that the key is not generated in a repeatable manner, that weakKeys was enabled, or that the result is null (not computable). If you can provide a reproducible test that would help in identifying the problem. – Ben Manes May 23 '17 at 12:08

0 Answers0