详解Spring boot使用Redis集群替换mybatis二级缓存
作者:White鱼 时间:2023-03-09 17:17:38
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
标签:spring,boot,redis
0
投稿
猜你喜欢
Android实时文件夹创建方法
2023-11-30 03:13:42
在Android环境下WebView中拦截所有请求并替换URL示例详解
2022-05-26 06:21:56
Spring Boot缓存实战 Caffeine示例
2021-11-15 17:39:34
解析C#拼接Json串的几种方法
2021-12-28 20:22:48
C#冒泡法排序算法实例分析
2023-08-13 18:03:42
Spring Cloud Alibaba使用Nacos作为注册中心和配置中心
2021-07-15 18:18:42
C#函数式编程中的惰性求值详解
2022-01-27 03:07:29
SpringBoot内置tomcat调优测试优化
2023-04-09 03:01:59
java中方法递归的简单示例
2022-11-25 11:45:12
dubbo服务链路跟踪方式
2023-08-24 09:54:21
Android编程之文件读写操作与技巧总结【经典收藏】
2023-10-16 02:20:52
C#使用iCSharpcode进行文件压缩实现方法
2022-09-28 06:25:32
Java应用开源框架实现简易web搜索引擎
2023-08-22 20:20:54
java实现科研信息管理系统
2022-05-13 02:49:41
Android Studio一直处于Building的两种解决方法
2022-06-13 23:05:22
通过实例解析JMM和Volatile底层原理
2023-05-20 19:10:48
60条Android开发注意事项与经验总结
2021-12-07 11:55:16
Maven安装及MyEclipse中使用Maven
2023-06-20 04:29:07
Android WebView无法弹出软键盘的原因及解决办法
2023-07-07 18:30:00
C#字符串使用密钥进行加解密
2023-12-27 19:10:17