目前,我有一个方法,使用Jooq存储方法在数据库上插入 . 每次存储记录时,它都会被添加到本地缓存中,该缓存基本上是一个带有Id的映射(我需要存储以供以后使用)和保存时生成的Id . 所以基本上:
private Map<String, Integer> cache = new HashMap<>();
public insert(List<Customer> customers>{
//some code to convert Customer to jooq generated CustomerRecord,
//which implements UpdatableRecord from Jooq
record.store();
cache.put(record.getFrontId(), record.getId());
}
public int find(String frontId) { return cache.get(frontId); }
它's all currently working, but the managing of all this is requiring a lot of effort. How to use a caching like this with Spring @Cacheable? I'从未使用它,但我尝试将 @CacheEvict(value="customer", key="#frontId")
添加到find方法中,但当调用它时缓存当然是空的 .
1 回答
这是一个例子...(参数自动为缓存键(您可以在Cacheable中使用(key =“#search.keyword)指定它):
缓存查看是否可以在其实现的缓存中找到customerkey,如果它具有密钥,则返回客户并且方法体不会被调用 .
如果缓存没有找到任何密钥条目,它会调用正文并返回客户,现在它也存储在缓存中 .
真正导入的是,不要忘记在main方法中激活缓存 .