没有合适的资源?快使用搜索试试~ 我知道了~
首页Mybatis-plus基于redis实现二级缓存过程解析
Mybatis-plus基于redis实现二级缓存过程解析
2.2k 浏览量
更新于2023-05-27
评论
收藏 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
- 粉丝: 8
- 资源: 954
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0