Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create simple cache with Caffeine

I'm trying to create a simple (non-loading) cache with Caffeine.

Cache<String, MyObject> countsCache =   
    CacheBuilder.newBuilder().build();

This fails to compile, with the Error reported:

Error:(42, 31) java: incompatible types: 
no instance(s) of type variable(s) K1,V1 exist so that org.elasticsearch.common.cache.Cache<K1,V1> conforms to com.github.benmanes.caffeine.cache.Cache<java.lang.String,com.foo.bar.MyObject>

Any suggestions would be greatly appreciated.

like image 505
L. Blanc Avatar asked Apr 30 '26 01:04

L. Blanc


1 Answers

It appears that you imported the ElasticSearch's Cache interface to assign to the result of the cache builder. The builder syntax you showed is Guava's CacheBuilder. Because many users would have Guava and might migrate, the builder is called Caffeine to reduce confusion.

You should be able to construct a cache like,

Cache<String, MyObject> countsCache = Caffeine.newBuilder().build();
like image 165
Ben Manes Avatar answered May 01 '26 15:05

Ben Manes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!