本文已参与「新人创作礼」活动,一起开启创作之路。
上一次的文章主要介绍了spring中缓存的使用,不过文中的案例都是以本地内存作为存储介质的,但是实际上我们的项目上线之后,基本上都会采用集群的方式进行部署,如果将数据存储在本地内存中,集群之间是无法共享的,我们可以将数据存储在redis中,从而实现缓存的共享,下面我们一起来看下Spring中@EnableCaching如何对接redis。
安装redis
下载地址:https://redis.io/download
pom.xml中引入redis配置
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.3</version>
</dependency>
项目中创建redis配置文件
新建com/tengteng/cache/demo2/redis.yml
,内容如下:
singleServerConfig: address:"redis://127.0.0.1:6379" password:null clientName:null database:7#选择使用哪个数据库0~15 idleConnectionTimeout:10000 connectTimeout:10000 timeout:3000 codec: class:"org.redisson.codec.JsonJacksonCodec"
创建redis相关的bean
packagecom.tengteng.cache.demo2; importorg.redisson.Redisson; importorg.redisson.api.RedissonClient; importorg.redisson.config.Config; importorg.redisson.spring.cache.RedissonSpringCacheManager; importorg.springframework.cache.CacheManager; importorg.springframework.cache.annotation.EnableCaching; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.ComponentScan; importjava.io.IOException; importjava.io.InputStream; importjava.util.Arrays; @ComponentScan @EnableCaching//@1 publicclassMainConfig2{ @Bean//@2 publicCacheManagercacheManager()throwsIOException{ RedissonSpringCacheManagercacheManager=newRedissonSpringCacheManager(this.redissonClient()); cacheManager.setCacheNames(Arrays.asList("cache1")); returncacheManager; } @Bean//@3 publicRedissonClientredissonClient()throwsIOException{ InputStreamis=MainConfig2.class.getResourceAsStream("/com/tengteng/cache/demo2/redis.yml"); Configconfig=Config.fromYAML(is); returnRedisson.create(config); } }
@1:开启spring cache功能。
@2:自定义spring中cache管理器,这个地方我们定义了一个redis类型的管理器,底层使用redis来作为缓存的存储介质。
@3:通过redis.yml配置文件来创建一个RedissonClient,用于和redis进行交互。
来个测试类
packagecom.tengteng.cache.demo2;
importorg.springframework.cache.annotation.Cacheable;
importorg.springframework.stereotype.Component;
importjava.util.Arrays;
importjava.util.List;
@Component
publicclassBookService{
@Cacheable(cacheNames="cache1",key="#root.targetClass.name+'-'+#root.method.name")
publicList<String>list(){
System.out.println("---模拟从db中获取数据---");
returnArrays.asList("java高并发","springboot","springcloud");
}
}
测试用例
@Test publicvoidtest7(){ AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(MainConfig2.class); BookServicebookService=context.getBean(BookService.class); System.out.println(bookService.list()); System.out.println(bookService.list()); { System.out.println("下面打印出cache1缓存中的key列表"); RedissonSpringCacheManagercacheManager=context.getBean(RedissonSpringCacheManager.class); RedissonCachecache1=(RedissonCache)cacheManager.getCache("cache1"); cache1.getNativeCache().keySet().stream().forEach(System.out::println); } }
运行输出
---模拟从db中获取数据--- [java高并发,springboot,springcloud] [java高并发,springboot,springcloud] 下面打印出cache1缓存中的key列表 com.tengteng.cache.demo2.BookService-list
此时数据已经进入redis了,我们用redis客户端工具RedisDesktopManager
来看一下。
RedisDesktopManager
下载地址
链接:https://pan.baidu.com/s/1Jw4N9BXi89SnBBARTikOCQ? 提取码:Teng


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)