首页 > 解决方案 > 为什么 Spring Boot 缓存配置不起作用

问题描述

我有一个简单的 Spring Boot 应用程序,它与自定义 jar 交互以从第三方应用程序获取数据。

我的 Spring Boot 应用程序类如下所示:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class TestApplication {
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

我有一个这样的控制器(它调用一个服务方法):

@RequestMapping(value = {"/getTestData.spr"}, method = RequestMethod.GET)
public JsonView getTestData(@RequestParam(value = "childProjectIdKeyName", required = false) String childProjectIdKeyName) {
   
   //Get All Test Case Data
   testRunsFromTracker = testRunProgressService.getAllTestRunListByProjects(childProjectIdKeyName);

   //Some other logic that returns a JSON View to the front-end
   return new JsonView(someManipulatedCollectionDataHere)
}

被调用的服务类如下所示:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class TestService {
@Cacheable (cacheNames = "testRun", key = "#childProjectIdKeyName")
   public List<TrackerItemDto> getAllTestRunListByProjects(String childProjectIdKeyName) {
      System.out.println(String.format("-----getAllTestRunListByProjects(%s)-----", childProjectIdKeyName));
      //-----some complex code that take a long time to execute everytime it is called-----//
}

我有我pom.xml的包含以下依赖项:

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

每次我调用在提到的调用的控制器时TestService,我都会打印控制台,说明我仍然缺少一些使缓存工作的东西。每次都调用服务方法,这在缓存实现之后是不期望的。

我在这里错过了什么吗?

标签: javaspringspring-bootcachingspring-cache

解决方案


推荐阅读