我必须实现Redis来进行缓存管理。我正在学习this教程,但问题是当我在控制器方法上使用@Cacheable注解时,它会给我一个404状态代码。
我的Controller方法如下:
@GetMapping("auth/cache/{id}")
@Cacheable("test")
public ServiceResponse<String> checkingCache(@PathVariable("id") Integer id){
return new ServiceResponse<>(new String("String with id "+id));
}当我删除cacheable注解时,该方法按预期工作。
我已经在我的机器上安装了redis,它在默认端口上运行。Redis配置如下:
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379我是不是漏掉了什么?任何帮助都将不胜感激,谢谢!
发布于 2018-10-25 16:03:33
您是否使用教程中描述的注释@EnableCaching启用了缓存。
此外,因为您需要为特定的id缓存,所以您必须提供该键来缓存。你的例子应该是:
@GetMapping("auth/cache/{id}")
@Cacheable(value = "test", key = "#id")
public ServiceResponse<String> checkingCache(@PathVariable("id") Integer id){
return new ServiceResponse<>(new String("String with id "+id));
}如果您没有@PathVariable,则可以不使用键来注释@Cachable
https://stackoverflow.com/questions/52983455
复制相似问题