import java.util.Arrays;
import java.util.List;
@Component
public class BookService {
@Cacheable(cacheNames = “cache1”, key = “#root.targetClass.name+‘-’+#root.method.name”)
public List list() {
System.out.println(“—模拟从db中获取数据—”);
return Arrays.asList(“java高并发”, “springboot”, “springcloud”);
}
}
测试用例
@Test
public void test7() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
BookService bookService = context.getBean(BookService.class);
System.out.println(bookService.list());
System.out.println(bookService.list());
{
System.out.println(“下面打印出cache1缓存中的key列表”);
RedissonSpringCacheManager cacheManager = context.getBean(RedissonSpringCacheManager.class);
RedissonCache cache1 = (RedissonCache) cacheManager.getCache(“cache1”);
cache1.getNativeCache().keySet().stream().forEach(System.out::println);
}
}
运行输出
—模拟从db中获取数据—
[java高并发, springboot, springcloud]
[java高并发, springboot, springcloud]
下面打印出cache1缓存中的key列表
com.javacode2018.cache.demo2.BookService-list
此时数据已经进入redis了,我们用redis客户端工具RedisDesktopManager
来看一下。
RedisDesktopManager
下载地址
链接:https://pan.baidu.com/s/1WCd-tk8dDDJnFIKciVIQsA
提取码:x728
解压之后,点击下面的直接运行