remove 移除 MybatisPlusRedisCache 二级缓存
This commit is contained in:
parent
8a93371baa
commit
5b3c390e08
@ -1,91 +0,0 @@
|
||||
package com.ruoyi.common.core.mybatisplus.cache;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.ruoyi.common.utils.RedisUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.cache.Cache;
|
||||
import org.springframework.data.redis.connection.RedisServerCommands;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* mybatis-redis 二级缓存
|
||||
*
|
||||
* 使用方法 配置文件开启 mybatis-plus 二级缓存
|
||||
* 在 XxxMapper.java 类上添加注解 @CacheNamespace(implementation = MybatisPlusRedisCache.class, eviction = MybatisPlusRedisCache.class)
|
||||
*
|
||||
* @deprecated 3.4.0删除 推荐使用spirng-cache
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
public class MybatisPlusRedisCache implements Cache {
|
||||
|
||||
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
|
||||
|
||||
private String id;
|
||||
|
||||
public MybatisPlusRedisCache(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) {
|
||||
if (value != null) {
|
||||
RedisUtils.setCacheObject(key.toString(), value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObject(Object key) {
|
||||
try {
|
||||
if (key != null) {
|
||||
return RedisUtils.getCacheObject(key.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error("缓存出错");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object removeObject(Object key) {
|
||||
if (key != null) {
|
||||
RedisUtils.deleteObject(key.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
log.debug("清空缓存");
|
||||
Collection<String> keys = RedisUtils.keys("*:" + this.id + "*");
|
||||
if (!CollectionUtils.isEmpty(keys)) {
|
||||
RedisUtils.deleteObject(keys);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
RedisTemplate<String, Object> redisTemplate = SpringUtil.getBean("redisTemplate");
|
||||
Long size = redisTemplate.execute(RedisServerCommands::dbSize);
|
||||
return size.intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadWriteLock getReadWriteLock() {
|
||||
return this.readWriteLock;
|
||||
}
|
||||
}
|
@ -2,11 +2,9 @@ package com.ruoyi.demo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.common.core.mybatisplus.cache.MybatisPlusRedisCache;
|
||||
import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus;
|
||||
import com.ruoyi.demo.domain.TestDemo;
|
||||
import com.ruoyi.demo.domain.vo.TestDemoVo;
|
||||
import org.apache.ibatis.annotations.CacheNamespace;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
@ -15,8 +13,6 @@ import org.apache.ibatis.annotations.Param;
|
||||
* @author Lion Li
|
||||
* @date 2021-07-26
|
||||
*/
|
||||
// 如使需切换数据源 请勿使用缓存 会造成数据不一致现象
|
||||
@CacheNamespace(implementation = MybatisPlusRedisCache.class, eviction = MybatisPlusRedisCache.class)
|
||||
public interface TestDemoMapper extends BaseMapperPlus<TestDemo> {
|
||||
|
||||
Page<TestDemoVo> customPageList(@Param("page") Page<TestDemo> page, @Param("ew") Wrapper<TestDemo> wrapper);
|
||||
|
Loading…
x
Reference in New Issue
Block a user