首页 > 解决方案 > 如何通过使用@Cacheable 注解实现以下行为?

问题描述

假设我有一个从 Mongo 获取数据的方法,可以缓存到 redis 中。

@Cacheable
public List<Values> cacheValues(List<String> input){
     // code that fetches the list based on input from mongodb
}

问题是有一个文档列表对应于 mongo db 中的每个特定输入。从 mongo 获取时,所有列表都合并为一个列表。有什么方法可以告诉@Cacheable 注解必须以相同的方式持久化和获取数据?或者有什么方法可以自定义@Cacheable 以便能够使用我们自己的redis 操作来获取和持久化?我正在使用弹簧数据 redis。

第二个问题:如何根据输入中的值创建密钥。例如:

@Cacheable(cacheNames = "cach1", key = "input[0] + …. input[input.size-1]")
    
        public List<Values> cacheValues(List<String> input){
    
             // code that fetches the list based on input from mongodb
    
        }

标签: javaspring-bootcachingredisspring-data-redis

解决方案


推荐阅读