8 changed files with 164 additions and 5 deletions
@ -0,0 +1,76 @@ |
|||
package com.ccsens.util.mybatis; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.ibatis.cache.Cache; |
|||
import org.springframework.data.redis.connection.RedisConnection; |
|||
import org.springframework.data.redis.core.RedisCallback; |
|||
import org.springframework.data.redis.core.RedisTemplate; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.util.concurrent.TimeUnit; |
|||
import java.util.concurrent.locks.ReadWriteLock; |
|||
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: whj |
|||
* @time: 2021/7/19 16:48 |
|||
*/ |
|||
@Slf4j |
|||
public class MybatisRedisCache implements Cache { |
|||
|
|||
private final String id; |
|||
@Resource(name="mybatisCache") |
|||
private RedisTemplate redisTemplate; |
|||
/** redis过期时间 单位:s */ |
|||
private static final long EXPIRE_TIME_IN_MINUTES = 30; |
|||
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); |
|||
|
|||
|
|||
public MybatisRedisCache(String id) { |
|||
if (id == null) { |
|||
throw new IllegalArgumentException("Cache instances require an ID"); |
|||
} |
|||
this.id = id; |
|||
log.debug("创建了mybatis的redis缓存"+id); |
|||
} |
|||
|
|||
@Override |
|||
public String getId() { |
|||
return id; |
|||
} |
|||
|
|||
@Override |
|||
public void putObject(Object key, Object value) { |
|||
redisTemplate.opsForValue().set(key, value, EXPIRE_TIME_IN_MINUTES, TimeUnit.SECONDS); |
|||
} |
|||
|
|||
@Override |
|||
public Object getObject(Object key) { |
|||
return redisTemplate.opsForValue().get(key); |
|||
} |
|||
|
|||
@Override |
|||
public Object removeObject(Object key) { |
|||
return redisTemplate.delete(key); |
|||
} |
|||
|
|||
@Override |
|||
public void clear() { |
|||
redisTemplate.execute((RedisCallback) connection -> { |
|||
//将数据库清空
|
|||
connection.flushDb(); |
|||
return null; |
|||
}); |
|||
} |
|||
|
|||
@Override |
|||
public int getSize() { |
|||
return 0; |
|||
} |
|||
|
|||
@Override |
|||
public ReadWriteLock getReadWriteLock() { |
|||
return readWriteLock; |
|||
} |
|||
} |
Loading…
Reference in new issue