博客
关于我
Spring中@EnableCaching如何集成redis
阅读量:333 次
发布时间:2019-03-04

本文共 3258 字,大约阅读时间需要 10 分钟。

上篇文章主要介绍了spring中缓存的使用,不过文中的案例都是以本地内存作为存储介质的,但是实际上我们的项目上线之后,基本上都会采用集群的方式进行部署,如果将数据存储在本地内存中,集群之间是无法共享的,我们可以将数据存储在redis中,从而实现缓存的共享,下面我们一起来看下Spring中@EnableCaching如何对接redis

安装redis

下载地址:https://redis.io/download

pom.xml中引入redis配置

    
org.redisson
    
redisson
    
3.13.3

项目中创建redis配置文件

新建com/javacode2018/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: 3000codec:  class: "org.redisson.codec.JsonJacksonCodec"

创建redis相关的bean

package com.javacode2018.cache.demo2;import org.redisson.Redisson;import org.redisson.api.RedissonClient;import org.redisson.config.Config;import org.redisson.spring.cache.RedissonSpringCacheManager;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import java.io.IOException;import java.io.InputStream;import java.util.Arrays;@ComponentScan@EnableCaching //@1public class MainConfig2 {    @Bean //@2    public CacheManager cacheManager() throws IOException {        RedissonSpringCacheManager cacheManager = new RedissonSpringCacheManager(this.redissonClient());        cacheManager.setCacheNames(Arrays.asList("cache1"));        return cacheManager;    }    @Bean //@3    public RedissonClient redissonClient() throws IOException {        InputStream is = MainConfig2.class.getResourceAsStream("/com/javacode2018/cache/demo2/redis.yml");        Config config = Config.fromYAML(is);        return Redisson.create(config);    }}

@1:开启spring cache功能。

@2:自定义spring中cache管理器,这个地方我们定义了一个redis类型的管理器,底层使用redis来作为缓存的存储介质。

@3:通过redis.yml配置文件来创建一个RedissonClient,用于和redis进行交互。

来个测试类

package com.javacode2018.cache.demo2;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;import java.util.Arrays;import java.util.List;@Componentpublic 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");    }}

测试用例

@Testpublic 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

解压之后,点击下面的直接运行

Spring中@EnableCaching如何集成redis

 

Spring中@EnableCaching如何集成redis

转载地址:http://foye.baihongyu.com/

你可能感兴趣的文章
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了
查看>>
云计算之路-阿里云上:奇怪的CPU 100%问题
查看>>
云计算之路-阿里云上:2014年6月12日12点IIS请求到达量突降
查看>>
上周热点回顾(6.9-6.15)
查看>>
上周热点回顾(6.16-6.22)
查看>>
上周热点回顾(6.23-6.29)
查看>>
上周热点回顾(10.20-10.26)
查看>>
上周热点回顾(2.16-2.22)
查看>>
上周热点回顾(3.2-3.8)
查看>>
[网站公告]3月10日23:00-4:00阿里云SLB升级,会有4-8次连接闪断
查看>>
.NET跨平台之旅:借助ASP.NET 5 Beta5的新特性显示CLR与操作系统信息
查看>>
上周热点回顾(7.27-8.2)
查看>>
上周热点回顾(9.28-10.4)
查看>>
.NET跨平台之旅:基于.NET Core改写EnyimMemcached,实现Linux上访问memcached缓存
查看>>
[网站公告]数据库服务器IOPS跑满造成网站不能正常访问
查看>>
上周热点回顾(3.28-4.3)
查看>>
上周热点回顾(5.2-5.8)
查看>>
上周热点回顾(5.9-5.15)
查看>>
上周热点回顾(8.8-8.14)
查看>>
.NET跨平台之旅:将示例站点升级至 .NET Core 1.1 Preview 1
查看>>