我使用 Spring boot 和 Ehcache 来缓存应用程序中的一些数据。 该应用程序是一个休息服务,缓存一些使用率较高的数据。
我们 Controller 中的代码如下所示:
@Cacheable("CategoryModels")
@GetMapping("/category/{companyId}")
public List<CategoryViewModel> getAllCategories(@PathVariable(value = "companyId", required = true) long companyId,
@RequestHeader("user") String user) {
//custom code here
}
现在,在某些情况下,用户会从服务器获取不同的数据集。有人可以在上述情况下解释一下吗?
如果数据库中的数据发生更改,我会刷新缓存,程序会自动将更新的数据更新到
为了刷新缓存,我使用自定义的编写方法:
Cache categoryCache = (Cache) manager.getCache("CategoryModels").getNativeCache();
categoryCache.removeAll();
categoryController.getAllCategories(company.getCompanyId(), null);
我对其他缓存有相同的行为,这些缓存的使用和刷新方式与上述缓存的使用方式相同。
最佳答案
您应该尝试使用以下方式参数化您的缓存定义:
@Cacheable(value="CategoryModels", key="{ #root.methodName, #companyId, #user.id }")
关于java - Spring boot @Cacheble 与 Ehcache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51703253/