[英]spring boot cache with ehcache Cannot find cache named xxx for Builder
[英]ehcache 3 cache-through with spring boot and JPA
我跟随Louis Jacomet和Aurelien Broszniowski一起进行“ Caching 101”(高速缓存101)操作,使用ehcache 3进行了高速缓存直通。
此演示文稿有一些亮点(但简要提及)。 其中之一是缓存密钥管理,这现在成为应用程序的责任。 由于创建的对象/数据是在第一个b / f处缓存的,因此它们是持久的(缓存的整个点),我们无法使用db生成的“ id”返回给我们(JPA)...向实体添加新字段以存储此缓存键。
因此,我创建了一个“密钥生成器”,用于维护一组长缓存密钥。 精细。 但是,当应用程序停止时,此Set消失。 因此,如果在应用程序运行时创建了任何对象/条目并将其持久保存在数据库中,则在启动时,这些记录需要插入到应用程序缓存中,并且还必须填充缓存键(Set)。
在我的示例代码中 ,我的rest控制器是与缓存交互的控制器,我添加了一种方法来实现此目的。 它调用repository.findAll()来填充实体列表(在这种情况下为Product)。 由于每个以前存在的实体也都保留了缓存密钥,因此我遍历此列表并填充缓存; 还是我想。
@PostConstruct
private void initializeCacheFromDB() {
List<Product> productList = new ArrayList<>();
repository.findAll().forEach(productList::add);
for (Product product : productList) {
// update the cache
// NOTE: below has no effect!
productCache.put(product.getCacheKey(), product);
// update/set the keys
CustomKeyGenerator.addKey(product.getCacheKey());
}
// does nothing!!!! ... no entries so keys not associated w/ Product(s)
//productCache.getAll(CustomKeyGenerator.getKeysSet());
System.out.println("done w/ initializeCacheFromDB()");
}
但是,何时,在控制台确认该应用程序之后。 已经启动,我导航到其他端点以获取说列表,其为空!
因此,我的问题是,如何在启动时用任何持久数据填充缓存。
使用BootstrapCacheLoader
请参阅http://www.ehcache.org/documentation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.