我正在尝试将Spring缓存添加到我的项目中。我在网上阅读了getting started和一些示例,看起来很简单,但是当我使用可缓存注释时,我的方法会导致 HTTP 404 错误。
这些是我的步骤:
将缓存依赖项添加到我的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
将@EnableCaching
注释添加到我的主类。
@SpringBootApplication
@EnableCaching
public class MyProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyProjectApplication.class, args);
}
}
将CacheManager Bean添加到我的项目中:
@Configuration
public class ConfigApplication {
@Bean
public CacheManager cacheManager() {
String[] cacheNames = { "videoInfo" };
return new ConcurrentMapCacheManager(cacheNames);
}
}
现在,在我喜欢缓存的方法中,我添加了@Cacheable("videoInfo")
注释。 (此方法的bean使用@RestController
注释)。我在@CacheEvict
添加了其他方法来重置缓存。
@Override
@Cacheable("videoInfo")
@RequestMapping(value = "/get-video-download", method = RequestMethod.POST, produces = "application/json")
public DownloadInfo getDownloadUrls(@RequestParam String videoId) {
DownloadInfo di = null;
di = downloadService.getDownloadInfo(videoId);
return di;
}
@CacheEvict(value = "videoInfo", allEntries = true)
@RequestMapping(value = "/get-video-download-reset-cache", method = RequestMethod.GET)
public void getDownloadUrlsResetCache() {
LOG.debug("Se ha limpiado la caché de videos correctamente");
}
然后,当我对此bean中的任何方法发出HTTP请求时,出现404错误。如果我在上述方法中评论这两个注释,我就不会看到错误的日志,并且一切正常。对此有何想法?