Java 将现有缓存转换为映射到Spring@Cacheable
目前,我有一个使用Jooq-store方法在数据库上进行插入的方法。每次存储一条记录时,它都会被添加到本地缓存中,该缓存基本上是一个带有来自前端的Id的映射,我需要存储该Id以供以后使用,并且该Id是在保存时生成的。所以基本上:Java 将现有缓存转换为映射到Spring@Cacheable,java,spring-boot,caching,jooq,Java,Spring Boot,Caching,Jooq,目前,我有一个使用Jooq-store方法在数据库上进行插入的方法。每次存储一条记录时,它都会被添加到本地缓存中,该缓存基本上是一个带有来自前端的Id的映射,我需要存储该Id以供以后使用,并且该Id是在保存时生成的。所以基本上: private Map<String, Integer> cache = new HashMap<>(); public insert(List<Customer> customers>{ //some code t
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); }
这一切目前都在发挥作用,但管理这一切需要付出很大的努力。如何在Spring@Cacheable中使用这样的缓存?我从来没用过,但我试着添加
@CacheReceiveValue=customer,key=frontId调用find方法,但缓存在调用时当然是空的。这是一个示例。。。参数自动为缓存键。您可以在可缓存中使用key=search.keyword指定该参数:
@Cacheable(value="customercache")
public Customer find(String customerkey) {
return //Load some customer;
}
缓存会查看他是否可以在实现的缓存中找到customerkey,如果它具有该键,则会返回customer,并且方法体不会被调用。
如果缓存没有找到任何键条目,它将调用主体并返回客户,现在它也存储在缓存中。
真正重要的是,不要忘记在main方法中激活缓存
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
保存整个客户实体不是过分吗?我只需要它的id,而你只需要返回那里的id