irf*_*san 4 java spring spring-boot
我的应用程序在 Spring boot 1.5.1 上
我已经查找了与缓存在 Spring Boot 中不起作用(从同一个类调用可缓存方法等)相关的所有常见问题,但我似乎仍然无法确定为什么我的方法没有缓存。我只是尝试使用 Spring 内置的简单缓存(即并发哈希图)。
设置:在 pom.xml 中我添加了这个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的配置类如下:
@SpringBootApplication
@Configuration
@EnableCaching
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个控制器类,它具有以下方法来获取版本列表。
@RequestMapping(value = "/getVersionList", method = RequestMethod.GET)
public JSONObject getVersionList() {
JSONObject retJSON = new JSONObject();
List<String> versions = fetchVersionService.getVersionList();
retJSON.put("versions", versions);
return retJSON;
}
Run Code Online (Sandbox Code Playgroud)
以及 FetchVersionService 类,其中我有可缓存方法来缓存此版本列表
@Cacheable("versions")
public List<String> getVersionList() {
System.out.println("If there is something in the cache, it should not hit here.");
return randomService.getVersions(); //another service class that gets versions from a remote location, which takes a long time
}
Run Code Online (Sandbox Code Playgroud)
每当我对此函数进行多次 GET 调用时,它总是进入该函数内部,即使我只希望它执行该函数一次。由于某种原因,它不缓存结果。我的设置哪里出了问题,有什么想法吗?多谢。
小智 10
在这个问题下的评论中 @irfanhasan 提到他们导入了错误的包,但没有明确的回应。我遇到了同样的问题。我尝试使用 @Cachable 注释,但它不起作用。我发现它被导入为:
import springfox.documentation.annotations.Cacheable
Run Code Online (Sandbox Code Playgroud)
代替:
import org.springframework.cache.annotation.Cacheable;
Run Code Online (Sandbox Code Playgroud)
仔细查找 IDE 导入。
| 归档时间: |
|
| 查看次数: |
11223 次 |
| 最近记录: |