Как читать кэш Redis при использовании @Cacheable? - PullRequest
0 голосов
/ 07 декабря 2018

Я разрабатываю Spring Boot + Redis пример с использованием @Cacheable.Я разработал конечные точки REST, которые кэшируют данные, как и ожидалось, но мне немного любопытно, что данные хранятся в Redis.

Может кто-нибудь подсказать

1)Как данные сохраняются

2) Как читать данные из redis для ключа Cacheable?

127.0.0.1:6379> KEYS *
1) "post-top::SimpleKey []"

Author.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Author implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
}

enter image description here

Post.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Post implements Serializable{
    private static final long serialVersionUID = 1L;

    private String id;
    private String title;
    private String description;
    private String image;
    private int shares;
    private Author author;
}

PostController.java

@RestController
@RequestMapping("/posts")
public class PostController {
    private static final Logger log = LoggerFactory.getLogger(PostController.class);

    @Autowired
    private PostService postService;

    @Cacheable(value = "post-single", key="#id", unless = "#result.shares < 500")
    @GetMapping("/{id}")
    public Post getPostByID(@PathVariable String id) throws PostNotFoundException {
        log.info("get post with id {}", id);
        return postService.getPostByID(id);
    }


    @CachePut(value = "post-single", key = "#post.id")
    @PutMapping("/update")
    public Post updatePostByID(@RequestBody Post post) throws PostNotFoundException {
        log.info("update post with id {}", post.getId());
        postService.updatePost(post);
        return post;
    }


    @CacheEvict(value = "post-single", key = "#id")
    @DeleteMapping("/delete/{id}")
    public void deletePostByID(@PathVariable String id) throws PostNotFoundException {
        log.info("delete post with id {}", id);
        postService.deletePost(id);
    }

    @Cacheable(value = "post-top")
    @GetMapping("/top")
    public List<Post> getTopPosts() {
        return postService.getTopPosts();
    }

    @CacheEvict(value = "post-top")
    @GetMapping("/top/evict")
    public void evictTopPosts() {
        log.info("Evict post-top");
    }
}
...