博客
关于我
Spring中@EnableCaching如何集成redis
阅读量:337 次
发布时间: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/

你可能感兴趣的文章
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>