详解Spring boot使用Redis集群替换mybatis二级缓存
作者:White鱼 发布时间:2023-03-09 17:17:38
标签:spring,boot,redis
1 . pom.xml添加相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<!-- 依赖 -->
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- redis相关 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependencies>
2 . 配置Redis集群,参考spring-data-redis官方文档
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {
/*
* spring.redis.cluster.nodes[0] = 127.0.0.1:7379
* spring.redis.cluster.nodes[1] = 127.0.0.1:7380
* ...
*/
List<String> nodes;
/**
* Get initial collection of known cluster nodes in format {@code host:port}.
*
* @return
*/
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
}
@Configuration
public class AppConfig {
/**
* Type safe representation of application.properties
*/
@Autowired ClusterConfigurationProperties clusterProperties;
public @Bean RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(
new RedisClusterConfiguration(clusterProperties.getNodes()));
}
}
3 . 自定义二级缓存类
public class RedisCache implements Cache {
private static final String PREFIX = "SYS_CONFIG:";
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
private String id;
private JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
private static RedisConnectionFactory redisConnectionFactory;
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
@Override
public String getId() {
return this.id;
}
@Override
public void putObject(Object key, Object value) {
RedisClusterConnection conn = redisConnectionFactory
.getClusterConnection();
if (key == null)
return;
String strKey = PREFIX + key.toString();
conn.set(strKey.getBytes(), jdkSerializer.serialize(value));
conn.close();
}
@Override
public Object getObject(Object key) {
if (key != null) {
String strKey = PREFIX + key.toString();
RedisClusterConnection conn = redisConnectionFactory
.getClusterConnection();
byte[] bs = conn.get(strKey.getBytes());
conn.close();
return jdkSerializer.deserialize(bs);
}
return null;
}
@Override
public Object removeObject(Object key) {
if (key != null) {
RedisClusterConnection conn = redisConnectionFactory
.getClusterConnection();
conn.del(key.toString().getBytes());
conn.close();
}
return null;
}
@Override
public void clear() {
// 关键代码,data更新时清理缓存
RedisClusterConnection conn = redisConnectionFactory
.getClusterConnection();
Set<byte[]> keys = conn.keys((PREFIX+"*").getBytes());
for (byte[] bs : keys) {
conn.del(bs);
}
conn.close();
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return 0;
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
public static void setRedisConnectionFactory(RedisConnectionFactory redisConnectionFactory) {
RedisCache.redisConnectionFactory = redisConnectionFactory;
}
}
使用一个Transfer类间接注入RedisConnectionFactory
@Component
public class RedisCacheTransfer {
@Autowired
public void setJedisConnectionFactory(
RedisConnectionFactory jedisConnectionFactory) {
RedisCache.setRedisConnectionFactory(jedisConnectionFactory);
}
}
4 . 在application.propreties中开启二级缓存
开启mybatis的二级缓存
spring.datasource.cachePrepStmts=true
5 . 基于注解的使用
@CacheNamespace(implementation = RedisCache.class)
public interface ConfigDaoMapper {
.....
}
来源:http://blog.csdn.net/soul_code/article/details/62422036?utm_source=tuicool&utm_medium=referral
0
投稿
猜你喜欢
- 1. 前言在Java开发中接触的开发者大多数不太注重对接口的测试,结果在联调对接中出现各种问题。也有的使用Postman等工具进行测试,虽然
- 前言:小伙伴说能不能用springboot整合一下mybatis多数据源不使用JPA进行数据库连接操作。那么说干就干创建一个springbo
- Android异常详情介绍这种异常我遇到以下两种情况: 1. java.lang.IllegalStateException: No wra
- 介绍Java命令模式(Command Pattern)是一种行为型设计模式,它将请求封装成一个对象,从而使不同的请求可以进行参数化,并支持请
- 1、动态SQL片段通过SQL片段达到代码复用 <!-- 动态条件分页查询 --> <sql i
- 在阻塞队里中,除了对元素进行增加和删除外,我们可以把元素的删除做一个延迟的处理,即使用DelayQueue的方法。本文就来和大家聊聊Java
- Pager.javapackage pers.kangxu.datautils.common;import java.io.Serializ
- OSS不支持通过一个网络地址来上传图片,所以若想将网络上的图片上传到OSS上需要走点弯路。1、通过链接将图片下载到本地的一个文件夹下面2、用
- 本文实例讲述了Java文件上传与文件下载实现方法。分享给大家供大家参考,具体如下:Java文件上传数据上传是客户端向服务器端上传数据,客户端
- 读取文件流时,经常会遇到乱码的现象,造成乱码的原因当然不可能是一个,这里主要介绍因为文件编码格式而导致的乱码的问题。首先,明确一点,文本文件
- 本文实例讲述了Android编程之消息机制。分享给大家供大家参考,具体如下:一、角色描述1.Looper: 一个线程可以产生一个Looper
- 一、@Value读取application.properties配置文件中的值application.properties配置文件fileN
- springboot项目部署平时我们在部署springboot打成jar方式部署得时候,大多数都会编写启动脚本,脚本有很多种写法,但大多数意
- FrameLayout 在这个布局中,所有的子元素都不能被指定放置的位置,他们统统防御这块区域的左上角, 并且后面的子元素直接覆盖在前面的子
- 目录Sonar概述一、 搭建sona服务二、idea配置三、 配置maven的setting.xml文件四、idea中 mvn sonar:
- SpringBoot线程池和Java线程池的用法和实现原理使用默认的线程池方式一:通过@Async注解调用public class Asyn
- 很多App都有这种效果,特别一些电商类的App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的。今天来简单地模仿一下,还自
- 一.方法的基本使用1.什么是方法方法是一个代码片段,类似于C语言中的函数2.方法基本语法基本语法 // 方法定义
- 以前用序列化都是一些方法需要才实现的,后来业务需求要深拷贝才去研究。参阅了别人博客得出一些总结。序列化是为了把Java对象转化为字节序列(字
- 日志输出是所有系统必备的,很多开发人员可能因为常常使用log4j而忽视了JDK logging模块,两者之间是否有联系?是怎样的联系?JDK