【问题标题】:@Cacheable Annotation gives 404@Cacheable 注解给出 404
【发布时间】:2019-03-29 17:20:28
【问题描述】:

我必须实现 Redis 来进行缓存管理。我关注this 教程,但问题是当我在控制器方法上使用 @Cacheable 注释时,它会给我一个 404 状态代码。

我的控制器方法如下:

@GetMapping("auth/cache/{id}")
@Cacheable("test")
public ServiceResponse<String> checkingCache(@PathVariable("id") Integer id){
    return new ServiceResponse<>(new String("String with id "+id));
}

当我删除可缓存注释时,该方法按预期工作。

我已经在我的机器上安装了 redis,它在默认端口上运行。 Redis配置如下:

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379

我有什么遗漏吗?任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: spring spring-boot caching redis


    【解决方案1】:

    您是否使用教程中描述的注释 @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2021-08-22
      • 2014-08-12
      • 1970-01-01
      • 2018-07-03
      • 2018-10-08
      • 2020-02-29
      相关资源
      最近更新 更多