Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit b6b2ff2

Browse files
committed
feat: add redis for spring cache
1 parent 1160fce commit b6b2ff2

File tree

5 files changed

+248
-0
lines changed

5 files changed

+248
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.code4everything.boot.cache;
2+
3+
import org.code4everything.boot.config.BootConfig;
4+
import org.code4everything.boot.web.mvc.exception.ExceptionFactory;
5+
import org.springframework.cache.Cache;
6+
import org.springframework.cache.support.SimpleValueWrapper;
7+
import org.springframework.http.HttpStatus;
8+
9+
import java.util.Objects;
10+
import java.util.concurrent.Callable;
11+
12+
/**
13+
* @author pantao
14+
* @since 2019/6/14
15+
**/
16+
public abstract class AbstractCache implements Cache {
17+
18+
private final String name;
19+
20+
private final Object nativeCache;
21+
22+
public AbstractCache(String name, Object nativeCache) {
23+
this.name = name;
24+
this.nativeCache = nativeCache;
25+
}
26+
27+
@Override
28+
public String getName() {
29+
return name;
30+
}
31+
32+
@Override
33+
public Object getNativeCache() {
34+
return nativeCache;
35+
}
36+
37+
@Override
38+
public ValueWrapper putIfAbsent(Object key, Object value) {
39+
ValueWrapper wrapper = get(key);
40+
if (Objects.isNull(wrapper)) {
41+
put(key, value);
42+
}
43+
return wrapper;
44+
}
45+
46+
protected ValueWrapper wrapValueIfNotNull(Object value) {
47+
return Objects.isNull(value) ? null : new SimpleValueWrapper(value);
48+
}
49+
50+
@SuppressWarnings("unchecked")
51+
protected <T> T convert2(Object value, Class<T> type) {
52+
return Objects.isNull(value) ? null : (T) value;
53+
}
54+
55+
@SuppressWarnings("unchecked")
56+
protected <T> T convert2(Object value, Callable<T> valueLoader) {
57+
if (Objects.isNull(value)) {
58+
try {
59+
return valueLoader.call();
60+
} catch (Exception e) {
61+
if (BootConfig.isDebug()) {
62+
throw ExceptionFactory.exception(HttpStatus.INTERNAL_SERVER_ERROR, "value loader error");
63+
}
64+
return null;
65+
}
66+
}
67+
return (T) value;
68+
}
69+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.code4everything.boot.cache.redis;
2+
3+
import org.code4everything.boot.cache.AbstractCache;
4+
import org.springframework.data.redis.core.RedisTemplate;
5+
6+
import java.util.Date;
7+
import java.util.concurrent.Callable;
8+
import java.util.concurrent.TimeUnit;
9+
10+
/**
11+
* Redis缓存
12+
*
13+
* @author pantao
14+
* @since 2019/6/14
15+
**/
16+
public class RedisCache extends AbstractCache {
17+
18+
private final RedisTemplate<String, Object> redisTemplate;
19+
20+
private final String keyPrefix;
21+
22+
private final long timeout;
23+
24+
public RedisCache(String name, RedisTemplate<String, Object> redisTemplate) {
25+
this(name, redisTemplate, Long.MAX_VALUE, name);
26+
}
27+
28+
public RedisCache(String name, RedisTemplate<String, Object> redisTemplate, long timeout) {
29+
this(name, redisTemplate, timeout, name);
30+
}
31+
32+
public RedisCache(String name, RedisTemplate<String, Object> redisTemplate, long timeout, String keyPrefix) {
33+
super(name, redisTemplate);
34+
this.keyPrefix = keyPrefix + ":";
35+
this.timeout = timeout;
36+
this.redisTemplate = redisTemplate;
37+
}
38+
39+
@Override
40+
public ValueWrapper get(Object key) {
41+
return wrapValueIfNotNull(redisTemplate.opsForValue().get(keyPrefix + key));
42+
}
43+
44+
@Override
45+
public <T> T get(Object key, Class<T> type) {
46+
return convert2(redisTemplate.opsForValue().get(keyPrefix + key), type);
47+
}
48+
49+
@Override
50+
public <T> T get(Object key, Callable<T> valueLoader) {
51+
return convert2(redisTemplate.opsForValue().get(keyPrefix + key), valueLoader);
52+
}
53+
54+
@Override
55+
public void put(Object key, Object value) {
56+
String newKey = keyPrefix + key;
57+
redisTemplate.opsForValue().set(newKey, value);
58+
redisTemplate.expire(newKey, timeout, TimeUnit.SECONDS);
59+
}
60+
61+
@Override
62+
public void evict(Object key) {
63+
redisTemplate.delete(keyPrefix + key);
64+
}
65+
66+
@Override
67+
public void clear() {
68+
redisTemplate.delete(redisTemplate.keys(keyPrefix + "*"));
69+
}
70+
71+
public void expireAt(String key, Date date) {
72+
redisTemplate.expireAt(keyPrefix + key, date);
73+
}
74+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.code4everything.boot.cache.redis;
2+
3+
import org.code4everything.boot.cache.CacheCreator;
4+
import org.code4everything.boot.module.redis.RedisTemplateUtils;
5+
6+
import java.util.Map;
7+
import java.util.Objects;
8+
9+
/**
10+
* Redis缓存创建者
11+
*
12+
* @author pantao
13+
* @since 2019/6/14
14+
**/
15+
public class RedisCacheCreator implements CacheCreator {
16+
17+
private final long timeout;
18+
19+
private final Map<String, String> prefixMap;
20+
21+
public RedisCacheCreator() {
22+
this(Long.MAX_VALUE, null);
23+
}
24+
25+
public RedisCacheCreator(Map<String, String> prefixMap) {
26+
this(Long.MAX_VALUE, prefixMap);
27+
}
28+
29+
public RedisCacheCreator(long timeout) {
30+
this(timeout, null);
31+
}
32+
33+
public RedisCacheCreator(long timeout, Map<String, String> prefixMap) {
34+
this.timeout = timeout;
35+
this.prefixMap = prefixMap;
36+
}
37+
38+
@Override
39+
public RedisCache createCache(String cacheName) {
40+
String prefix = Objects.isNull(prefixMap) ? cacheName : prefixMap.getOrDefault(cacheName, cacheName);
41+
return new RedisCache(cacheName, RedisTemplateUtils.newTemplate(), timeout, prefix);
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.code4everything.boot.cache.redis;
2+
3+
import cn.hutool.core.util.ObjectUtil;
4+
import org.code4everything.boot.cache.BootCacheManager;
5+
import org.springframework.cache.Cache;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.Date;
10+
11+
/**
12+
* Redis缓存管理器
13+
*
14+
* @author pantao
15+
* @since 2019/6/14
16+
**/
17+
public class RedisCacheManager extends BootCacheManager {
18+
19+
public RedisCacheManager(RedisCacheCreator redisCacheCreator) {
20+
this(redisCacheCreator, 16);
21+
}
22+
23+
public RedisCacheManager(RedisCacheCreator redisCacheCreator, int capacity) {
24+
super(redisCacheCreator, capacity);
25+
}
26+
27+
public RedisCacheManager(Collection<RedisCache> caches, RedisCacheCreator redisCacheCreator) {
28+
super(new ArrayList<>(caches), redisCacheCreator);
29+
}
30+
31+
public RedisCacheManager(Collection<RedisCache> caches) {
32+
this(caches, null);
33+
}
34+
35+
public void expireAt(String cacheName, String key, Date date) {
36+
Cache cache = getCache(cacheName);
37+
if (ObjectUtil.isNotNull(cache)) {
38+
((RedisCache) cache).expireAt(key, date);
39+
}
40+
}
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.code4everything.boot.common;
2+
3+
import cn.hutool.core.lang.Console;
4+
import com.alibaba.fastjson.JSONObject;
5+
import com.google.common.collect.Lists;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
/**
11+
* @author pantao
12+
* @since 2019/6/14
13+
**/
14+
public class JsonTest {
15+
16+
@Test
17+
public void testJson() {
18+
List<String> list = Lists.newArrayList("a", "b", "c");
19+
Console.log(JSONObject.parse(JSONObject.toJSONString(list)));
20+
}
21+
}

0 commit comments

Comments
 (0)