我需要初始化需要包含的缓存
String in key
List<Object> in value
所以我有 CacheHelper 类
public class CacheHelper {
private CacheManager cacheManager;
private Cache<String,List<Person>> cacheDataList;
private static final String CACHE_PERSON="cache_key_person";
public CacheHelper() {
}
public void putInCacheFromDb(){
System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
cacheManager= CacheManagerBuilder
.newCacheManagerBuilder().build();
cacheManager.init();
cacheDataList = cacheManager
.createCache("cacheOfPersonList", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,Person.class,
ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
TimeUnit.SECONDS))));
}
public void putInList(List<Person> personList){
System.out.println("putting list in cache");
cacheDataList.put(CACHE_PERSON,personList);
}
}
但是在这行代码中,我无法将对象转换为列表 String.class,Person.class,它需要是 String.class,List:
cacheDataList = cacheManager
.createCache("cacheOfPersonList", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,Person.class,
ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
TimeUnit.SECONDS))));
但我收到错误:
Incompatible types. Required Cache<String, List<Person>> but 'createCache' was inferred to Cache<K, V>: Incompatible equality constraint: List<Person> and Person
我需要将 List 存储在一个键中。如何初始化它?