网站首页 > 开源技术 正文
上篇文章主要介绍了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/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: 3000
codec:
  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 //@1
public 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;
@Component
public class BookService {
    @Cacheable(cacheNames = "cache1", key = "#root.targetClass.name+'-'+#root.method.name")
    public List<String> 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
解压之后,点击下面的直接运行
猜你喜欢
- 2024-09-30 Redis window安装教程(redis安装配置 windows)
 - 2024-09-30 开发者笔记:Windows下搭建简易单机Redis集群测试环境
 - 2024-09-30 基于标准oauth2权限模型开发框架 ybg_auth
 - 2024-09-30 Java项目能力之「NoSQL学习文章大集合150篇」
 - 2024-09-30 十分钟教你快速安装redis(redis安装方法)
 - 2024-09-30 「快速阅读」Redis - 可视化(redis key分析)
 - 2024-09-30 一款免费全新稳定Redis桌面连接工具Another Redis Desktop Manager
 - 2024-09-30 第一次和Redis的亲密接触(redis连接协议)
 - 2024-09-30 帅气逼人的 Redis 可视化工具,功能强大
 - 2024-09-25 作为一名程序员,哪些常用的办公软件值得一试?
 
欢迎 你 发表评论:
- 1588℃北京那些看上去很牛的车牌们!(北京厉害车牌)
 - 1107℃2025年度视频去水印软件TOP5对比:哪款最值得用
 - 683℃《我的世界》不同版本的差异 ——新手向
 - 595℃新疆话里的“虫子”
 - 515℃中兴光猫 Telnet下设置大全(中兴光猫命令大全)
 - 513℃蓝牙设备配对失败的系统性解决方案与技术解析
 - 508℃未备份电脑文件数据恢复的七种方法
 - 488℃工艺管道常用英文缩写 英汉对照
 
- 最近发表
 
- 标签列表
 - 
- jdk (81)
 - putty (66)
 - rufus (78)
 - 内网穿透 (89)
 - okhttp (70)
 - powertoys (74)
 - windowsterminal (81)
 - netcat (65)
 - ghostscript (65)
 - veracrypt (65)
 - asp.netcore (70)
 - wrk (67)
 - aspose.words (80)
 - itk (80)
 - ajaxfileupload.js (66)
 - sqlhelper (67)
 - express.js (67)
 - phpmailer (67)
 - xjar (70)
 - redisclient (78)
 - wakeonlan (66)
 - tinygo (85)
 - startbbs (72)
 - webftp (82)
 - vsvim (79)
 
 

本文暂时没有评论,来添加一个吧(●'◡'●)