-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMybatisRedisCache.java
129 lines (112 loc) · 3.97 KB
/
MybatisRedisCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package site.yuyanjia.template.common.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.util.Assert;
import site.yuyanjia.template.common.contant.RedisKeyContant;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* mybatis redis 二级缓存
*
* @author seer
* @date 2018/3/13 14:17
*/
@Slf4j
public class MybatisRedisCache implements Cache {
private final String id;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private static RedisTemplate redisTemplate;
public static void setRedisTemplate(RedisTemplate redisTemplate) {
MybatisRedisCache.redisTemplate = redisTemplate;
}
public MybatisRedisCache(final String id) {
Assert.notNull(id, "mybatis redis cache need an id.");
this.id = id;
log.debug("mybatis redis cache ID: {}", id);
}
@Override
public String getId() {
return this.id;
}
@Override
public void putObject(Object key, Object value) {
log.debug("mybatis redis cache put. ID={} K={} V={}", id, key, value);
redisTemplate.opsForValue().set(generateKey(key), value);
}
@Override
public Object getObject(Object key) {
Assert.notNull(redisTemplate, "redisTemplate is null");
log.debug("mybatis redis cache get. ID={} K={}", id, key);
return redisTemplate.opsForValue().get(generateKey(key));
}
@Override
public Object removeObject(Object key) {
log.debug("mybatis redis cache remove. ID={} K={}", id, key);
Object result = redisTemplate.opsForValue().get(generateKey(key));
redisTemplate.delete(generateKey(key));
return result;
}
@Override
public void clear() {
log.debug("mybatis redis cache clear. ID={}", id);
RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
Assert.notNull(redisConnection, "redisConnection is null");
Cursor<byte[]> cursor = redisConnection.scan(ScanOptions.scanOptions()
.match(generateKey("*"))
.count(Integer.MAX_VALUE)
.build());
while (cursor.hasNext()) {
redisConnection.del(cursor.next());
}
try {
cursor.close();
} catch (IOException e) {
log.error("mybatis redis cache clear exception", e);
} finally {
redisConnection.close();
}
}
@Override
public int getSize() {
AtomicInteger count = new AtomicInteger(0);
RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
Assert.notNull(redisConnection, "redisConnection is null");
Cursor<byte[]> cursor = redisConnection.scan(ScanOptions.scanOptions()
.match(generateKey("*"))
.count(Integer.MAX_VALUE)
.build());
while (cursor.hasNext()) {
count.getAndIncrement();
}
try {
cursor.close();
} catch (IOException e) {
log.error("mybatis redis cache get size exception", e);
} finally {
redisConnection.close();
}
return count.get();
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
/**
* 重组key
* 区别其他使用环境的key
*
* @param key
* @return
*/
private String generateKey(Object key) {
String keyStr = RedisKeyContant.MYBATIS_CACHE_PREFIX + id + ":" + key;
keyStr = keyStr.replaceAll("\\n", "").replaceAll(" ", "");
return keyStr;
}
}