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("清空缓存");
redisTemplate = getRedisTemplate();
redisTemplate.delete(id.toString());
}

@Override
public int getSize() {
redisTemplate = getRedisTemplate();
Long size = redisTemplate.opsForHash().size(id.toString());
return size.intValue();
}

@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
}
 

3. 在mapper上加上注解@CacheNamespace或者在Mpper.xml中加入 <cache type="com.redis.shaobing.utils.MybatisRedisCache"/>

两种方式根据实际情况二选一即可!

@CacheNamespace(implementation= MybatisRedisCache.class,eviction=MybatisRedisCache.class)
@Mapper
public interface SysConfigDao extends BaseMapper<SysConfig> {}

如果调用该mapper下的方法,那么会使用redis缓存

 

 

 

 

posted @ 2020-07-18 10:42  william_zhao  阅读(3013)  评论(0编辑  收藏  举报