没有合适的资源?快使用搜索试试~ 我知道了~
首页Mybatis-plus基于redis实现二级缓存过程解析
Mybatis-plus基于redis实现二级缓存过程解析
8 下载量 94 浏览量
更新于2023-05-15
评论
收藏 97KB PDF 举报
主要介绍了Mybatis-plus基于redis实现二级缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
资源详情
资源评论
资源推荐
Mybatis-plus基于基于redis实现二级缓存过程解析实现二级缓存过程解析
主要介绍了Mybatis-plus基于redis实现二级缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1. mybatis-plus开启二级缓存开启二级缓存
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.222.155:3306/sys?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=utf-8
username: root
password: 123456
redis:
host: 39.104.203.155
port: 6380
password: 123456
database: 1
timeout: 2000ms # 连接超时时间(毫秒)默认是2000ms
lettuce:
pool:
max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 100 # 连接池中的最大空闲连接
min-idle: 50 # 连接池中的最小空闲连接
shutdown-timeout: 100ms
# sentinel: # 哨兵模式
# master: mymaster
# nodes: 192.168.222.155:26379,192.168.222.155:26380,192.168.222.155:26381
mybatis-plus: mapper-locations: classpath*:/mapper/*.xml type-aliases-package: com.redis.shaobing.entity global-config: db-config: id-type: auto table-underline: true configuration: cache-enabled: true map-underscore-to-camel-case: true
2. 自定义自己的缓存管理自定义自己的缓存管理
package com.redis.shaobing.utils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* @author shuangyueliao
* @create 2019/9/10 14:02
* @Version 0.1
*/
@Slf4j
public class MybatisRedisCache implements Cache {
// 读写锁
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
//这里使用了redis缓存,使用springboot自动注入
private RedisTemplate<String, Object> redisTemplate;
private String id;
public MybatisRedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
public RedisTemplate<String, Object> getRedisTemplate() {
redisTemplate = (RedisTemplate<String, Object>) ApplicationContextUtils.getBean("redisTemplate");
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
//使用StringRedisSerializer来序列化和反序列化redis的key值
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Override
public String getId() {
return this.id;
}
@Override
public void putObject(Object key, Object value) {
redisTemplate = getRedisTemplate();
if (value != null) {
redisTemplate.opsForHash().put(id.toString(), key.toString(), value);
}
}
@Override
public Object getObject(Object key) {
redisTemplate = getRedisTemplate();
try {
if (key != null) {
return redisTemplate.opsForHash().get(id.toString(), key.toString());
}
} catch (Exception e) {
e.printStackTrace();
log.error("缓存出错 ");
}
return null;
}
@Override
public Object removeObject(Object key) {
redisTemplate = getRedisTemplate();
if (key != null) {
redisTemplate.delete(key.toString());
}
return null;
}
@Override
public void clear() {
System.out.println("清空缓存");
log.debug("清空缓存");
weixin_38503448
- 粉丝: 10
- 资源: 953
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0