@CachePut后,@Cacheable对象在缓存中丢失

@Cacheable object is lost in cache after @CachePut

提问人:asayke 提问时间:11/8/2023 最后编辑:asayke 更新时间:11/8/2023 访问量:25

问:

    @GetMapping("/{id}")
    @Cacheable(value = "guests", key = "#id")
    @RolesAllowed({"MANAGER", "ADMIN"})
    public ResponseEntity<GuestResponse> findById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(guestService.findById(id));
    }

    @CachePut(value = "guests", key = "#id")
    @PutMapping("/{id}")
    @RolesAllowed({"MANAGER", "ADMIN"})
    public ResponseEntity<HttpStatus> update(@RequestBody Map<Object, Object> fields, @PathVariable("id") Long id) {
        guestService.update(id, fields);
        return ResponseEntity.ok(HttpStatus.OK);
    }

对象通常按 id 缓存,但@CachePut后,尽管数据库中发生了更改,但无法再从缓存中找到该对象。作为回应,我得到了 200 个没有尸体。

java spring-boot 缓存 caffeine-cache

评论

0赞 M. Deinum 11/8/2023
您同时缓存了两者并在同一个缓存中,这显然是行不通的。HttpStatusGuestResponse

答: 暂无答案