Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 0b155aa

Browse files
committed
#3 优化自定义序列化支持以及单元测试
1 parent 1671300 commit 0b155aa

File tree

8 files changed

+113
-15
lines changed

8 files changed

+113
-15
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ service:
77
before_script:
88
- sudo redis-server /etc/redis/redis.conf --port 6379
99
script:
10-
- mvn -Dcodec.lib.dir=${PWD}/lib/ test
10+
- mvn -Dredis.manager.codec.lib.dir=${PWD}/lib/ test
1111
after_success:
1212
- bash <(curl -s https://codecov.io/bash)
1313
cache:

redis-manager-core/src/main/java/org/hswebframework/redis/manager/AbstractRedisClientRepository.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
@Slf4j
2626
public abstract class AbstractRedisClientRepository implements RedisClientRepository {
2727

28-
protected Map<String, Cache> clientMap = new ConcurrentHashMap<>();
28+
protected Map<String, Cache> clientCache = new ConcurrentHashMap<>();
2929

30-
private ClassLoader classLoader;
30+
protected ClassLoader classLoader;
3131

3232
@SneakyThrows
3333
public AbstractRedisClientRepository() {
34-
File file = new File("./lib");
34+
File file = new File(System.getProperty("redis.manager.codec.lib.dir","./lib"));
3535
if (!file.exists()) {
3636
file.mkdir();
3737
}
38-
classLoader = new CodecClassLoader(file.toURI().toURL());
38+
classLoader = new CodecClassLoader(file);
3939
}
4040

4141
@Override
@@ -44,21 +44,21 @@ public RedissonClient getRedissonClient(String id, int database) {
4444
}
4545

4646
protected void updateCodec(String clientId, Map<String, RedisClient.CodecConfig> configMap) {
47-
if (clientMap.get(clientId) != null) {
47+
if (clientCache.get(clientId) != null) {
4848
getCache(clientId).reloadCodec(configMap);
4949
}
5050
}
5151

5252
protected Cache getCache(String id) {
5353
RedisClient client = findById(id);
5454
Objects.requireNonNull(client, "客户端不存在");
55-
Cache cache = clientMap.get(id);
55+
Cache cache = clientCache.get(id);
5656
if (cache == null || !cache.clientConf.equals(client)) {
5757
if (cache != null) {
5858
cache.close();
5959
}
6060
cache = createCache(client);
61-
clientMap.put(id, cache);
61+
clientCache.put(id, cache);
6262
}
6363
return cache;
6464
}
@@ -71,7 +71,7 @@ protected Cache createCache(RedisClient client) {
7171

7272
@Override
7373
public Codec getCodec(String clientId, String key) {
74-
return Optional.ofNullable(clientMap.get(clientId))
74+
return Optional.ofNullable(clientCache.get(clientId))
7575
.map(client -> client.getCodec(key))
7676
.orElseThrow(NullPointerException::new);
7777
}

redis-manager-core/src/main/java/org/hswebframework/redis/manager/InMemoryRedisClientRepository.java

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
*/
1414
public class InMemoryRedisClientRepository extends AbstractRedisClientRepository {
1515

16+
17+
@Override
18+
public RedisClient remove(String clientId) {
19+
RedisClient client = repository.remove(clientId);
20+
Cache cache = clientCache.get(clientId);
21+
if (null != cache) {
22+
cache.close();
23+
}
24+
return client;
25+
}
26+
1627
@Getter
1728
@Setter
1829
private Map<String, RedisClient> repository = new ConcurrentHashMap<>();

redis-manager-core/src/main/java/org/hswebframework/redis/manager/RedisClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class RedisClient {
2323
private String name;
2424

2525
//客户端分组
26-
private String group;
26+
private String group="default";
2727

2828
//备注
2929
private String comments;

redis-manager-core/src/main/java/org/hswebframework/redis/manager/RedisClientRepository.java

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public interface RedisClientRepository {
1313

1414
RedisClient saveOrUpdate(RedisClient client);
1515

16+
RedisClient remove(String clientId);
17+
1618
List<RedisClient> allClients();
1719

1820
RedisClient findById(String clientId);

redis-manager-core/src/main/java/org/hswebframework/redis/manager/codec/CodecClassLoader.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.hswebframework.redis.manager.codec;
22

33
import io.vavr.API;
4-
import io.vavr.CheckedFunction1;
54

65
import java.io.File;
76
import java.net.URL;
@@ -11,7 +10,7 @@
1110
public class CodecClassLoader extends URLClassLoader {
1211

1312
public CodecClassLoader(File... urls) {
14-
super(Stream.of(urls)
13+
this(Stream.of(urls)
1514
.flatMap(API.unchecked(file -> {
1615
if (file.isDirectory()) {
1716
File[] children = file.listFiles();
@@ -24,12 +23,16 @@ public CodecClassLoader(File... urls) {
2423
}
2524

2625
public CodecClassLoader(String... urls) {
27-
super(Stream.of(urls)
26+
this(Stream.of(urls)
2827
.map(API.<String, URL>unchecked(URL::new))
2928
.toArray(URL[]::new));
3029
}
3130

3231
public CodecClassLoader(URL... urls) {
33-
super(urls);
32+
this(CodecClassLoader.class.getClassLoader(), urls);
33+
}
34+
35+
public CodecClassLoader(ClassLoader parent, URL... urls) {
36+
super(urls, parent);
3437
}
3538
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.hswebframework.redis.manager
2+
3+
import io.netty.buffer.ByteBuf
4+
import org.hswebframework.redis.manager.codec.CodecType
5+
import org.hswebframework.web.bean.FastBeanCopier
6+
import org.redisson.api.RBucket
7+
import org.redisson.client.codec.Codec
8+
import org.redisson.client.handler.State
9+
import spock.lang.Specification
10+
11+
class InMemoryRedisClientRepositoryTest extends Specification {
12+
13+
def repository = new InMemoryRedisClientRepository();
14+
15+
def setup() {
16+
repository.getRepository()
17+
.put("test", new RedisClient(name: "test",
18+
group: "default",
19+
comments: "test",
20+
address: "redis://localhost:6379",
21+
password: null,
22+
codecConfig: ["test-data": new RedisClient.CodecConfig(keyCodec: CodecType.string, valueCodec: CodecType.fst)]))
23+
}
24+
25+
def cleanup() {
26+
repository.remove("test")
27+
}
28+
29+
def doEncodeDecode(Codec c, Object o) {
30+
ByteBuf buf = c.getValueEncoder().encode(o)
31+
def decode = c.getValueDecoder().decode(buf, new State(false))
32+
return decode == o
33+
}
34+
35+
def "测试redis客户端获取和删除"() {
36+
given: "获取配置的客户端"
37+
def redisClient = repository.findById("test");
38+
when: "获取成功"
39+
redisClient != null
40+
then: "删除客户端"
41+
def old = repository.remove("test");
42+
expect: "删除成功"
43+
old != null
44+
repository.findById("test") == null
45+
46+
}
47+
48+
def initRedissonClientSuccess(String clientId, int database) {
49+
return repository.getRedissonClient(clientId, database) != null;
50+
}
51+
52+
def "测试自定义序列化"() {
53+
def bucket = repository.getRedissonClient("test", 0)
54+
.getBucket("test-data", repository.getCodec("test", "test-data"));
55+
56+
given: "从自定义的jar中初始化类并放入redis"
57+
def testBean = repository.classLoader.loadClass("org.hswebframework.redis.manager.beans.TestBean");
58+
testBean.with {
59+
id: "test"
60+
name: "test"
61+
age: 20
62+
}
63+
bucket.set(testBean)
64+
expect: "设置成功"
65+
bucket.get() != null
66+
bucket.get()==testBean
67+
68+
}
69+
70+
def "测试初始化redis客户端"() {
71+
given: "初始化客户端"
72+
initRedissonClientSuccess(clientId, database) == success
73+
where: "初始化结果"
74+
clientId | database | success
75+
"test" | 0 | true
76+
"test" | 1 | true
77+
"test" | 2 | true
78+
"test" | 3 | true
79+
"test" | 4 | true
80+
"test" | 5 | true
81+
}
82+
}

redis-manager-core/src/test/groovy/org/hswebframework/redis/manager/codec/CodecClassLoaderTest.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CodecClassLoaderTest extends Specification {
1010

1111
def "测试自定义类加载"() {
1212
given:
13-
def classLoader = new CodecClassLoader(new File(System.getProperty("codec.lib.dir", "./lib")))
13+
def classLoader = new CodecClassLoader(new File(System.getProperty("redis.manager.codec.lib.dir", "./lib")))
1414

1515
and:
1616
def testBean = classLoader.loadClass("org.hswebframework.redis.manager.beans.TestBean");

0 commit comments

Comments
 (0)