我正在尝试使用 Caffeine 创建一个简单的(非加载)缓存。
Cache<String, MyObject> countsCache =
CacheBuilder.newBuilder().build();
编译失败,并报告错误:
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>
如有任何建议,我们将不胜感激。
最佳答案
看来您导入了 ElasticSearch 的 Cache 接口(interface)来分配给缓存构建器的结果。您显示的构建器语法是 Guava 的 CacheBuilder
。由于许多用户都会拥有 Guava 并且可能会迁移,因此该构建器被称为 Caffeine
以减少困惑。
您应该能够构建一个类似的缓存,
Cache<String, MyObject> countsCache = Caffeine.newBuilder().build();
关于java - 无法使用 Caffeine 创建简单缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37355417/