java - Spring boot @Cacheble 与 Ehcache

标签 java spring-boot ehcache

我使用 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/

相关文章:

java - 在 Java 中更改表格单元格颜色

java - 如何使用 Java 邮件 API 保存已发送邮件?

java - 如何停止通过实现可运行接口(interface)创建的线程?

spring-boot - 文件系统中的spring-boot Persistent h2database

java - 创建一个 Spring Boot 应用程序,但无法让 @autowired 工作

java - JPA 将 native 查询结果映射到非实体 DTO

grails - Grails ehcache失效不起作用

java - 如何检测WinPcap库是否安装?

java - Tomcat 等待加入 jgroups 集群

spring - 在 Spring 4 中使用 ehcache 3