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

Commit

Permalink
#3 优化自定义序列化支持以及单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Sep 28, 2018
1 parent 1671300 commit 0b155aa
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ service:
before_script:
- sudo redis-server /etc/redis/redis.conf --port 6379
script:
- mvn -Dcodec.lib.dir=${PWD}/lib/ test
- mvn -Dredis.manager.codec.lib.dir=${PWD}/lib/ test
after_success:
- bash <(curl -s https://codecov.io/bash)
cache:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
@Slf4j
public abstract class AbstractRedisClientRepository implements RedisClientRepository {

protected Map<String, Cache> clientMap = new ConcurrentHashMap<>();
protected Map<String, Cache> clientCache = new ConcurrentHashMap<>();

private ClassLoader classLoader;
protected ClassLoader classLoader;

@SneakyThrows
public AbstractRedisClientRepository() {
File file = new File("./lib");
File file = new File(System.getProperty("redis.manager.codec.lib.dir","./lib"));
if (!file.exists()) {
file.mkdir();
}
classLoader = new CodecClassLoader(file.toURI().toURL());
classLoader = new CodecClassLoader(file);
}

@Override
Expand All @@ -44,21 +44,21 @@ public RedissonClient getRedissonClient(String id, int database) {
}

protected void updateCodec(String clientId, Map<String, RedisClient.CodecConfig> configMap) {
if (clientMap.get(clientId) != null) {
if (clientCache.get(clientId) != null) {
getCache(clientId).reloadCodec(configMap);
}
}

protected Cache getCache(String id) {
RedisClient client = findById(id);
Objects.requireNonNull(client, "客户端不存在");
Cache cache = clientMap.get(id);
Cache cache = clientCache.get(id);
if (cache == null || !cache.clientConf.equals(client)) {
if (cache != null) {
cache.close();
}
cache = createCache(client);
clientMap.put(id, cache);
clientCache.put(id, cache);
}
return cache;
}
Expand All @@ -71,7 +71,7 @@ protected Cache createCache(RedisClient client) {

@Override
public Codec getCodec(String clientId, String key) {
return Optional.ofNullable(clientMap.get(clientId))
return Optional.ofNullable(clientCache.get(clientId))
.map(client -> client.getCodec(key))
.orElseThrow(NullPointerException::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
*/
public class InMemoryRedisClientRepository extends AbstractRedisClientRepository {


@Override
public RedisClient remove(String clientId) {
RedisClient client = repository.remove(clientId);
Cache cache = clientCache.get(clientId);
if (null != cache) {
cache.close();
}
return client;
}

@Getter
@Setter
private Map<String, RedisClient> repository = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RedisClient {
private String name;

//客户端分组
private String group;
private String group="default";

//备注
private String comments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface RedisClientRepository {

RedisClient saveOrUpdate(RedisClient client);

RedisClient remove(String clientId);

List<RedisClient> allClients();

RedisClient findById(String clientId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.hswebframework.redis.manager.codec;

import io.vavr.API;
import io.vavr.CheckedFunction1;

import java.io.File;
import java.net.URL;
Expand All @@ -11,7 +10,7 @@
public class CodecClassLoader extends URLClassLoader {

public CodecClassLoader(File... urls) {
super(Stream.of(urls)
this(Stream.of(urls)
.flatMap(API.unchecked(file -> {
if (file.isDirectory()) {
File[] children = file.listFiles();
Expand All @@ -24,12 +23,16 @@ public CodecClassLoader(File... urls) {
}

public CodecClassLoader(String... urls) {
super(Stream.of(urls)
this(Stream.of(urls)
.map(API.<String, URL>unchecked(URL::new))
.toArray(URL[]::new));
}

public CodecClassLoader(URL... urls) {
super(urls);
this(CodecClassLoader.class.getClassLoader(), urls);
}

public CodecClassLoader(ClassLoader parent, URL... urls) {
super(urls, parent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.hswebframework.redis.manager

import io.netty.buffer.ByteBuf
import org.hswebframework.redis.manager.codec.CodecType
import org.hswebframework.web.bean.FastBeanCopier
import org.redisson.api.RBucket
import org.redisson.client.codec.Codec
import org.redisson.client.handler.State
import spock.lang.Specification

class InMemoryRedisClientRepositoryTest extends Specification {

def repository = new InMemoryRedisClientRepository();

def setup() {
repository.getRepository()
.put("test", new RedisClient(name: "test",
group: "default",
comments: "test",
address: "redis://localhost:6379",
password: null,
codecConfig: ["test-data": new RedisClient.CodecConfig(keyCodec: CodecType.string, valueCodec: CodecType.fst)]))
}

def cleanup() {
repository.remove("test")
}

def doEncodeDecode(Codec c, Object o) {
ByteBuf buf = c.getValueEncoder().encode(o)
def decode = c.getValueDecoder().decode(buf, new State(false))
return decode == o
}

def "测试redis客户端获取和删除"() {
given: "获取配置的客户端"
def redisClient = repository.findById("test");
when: "获取成功"
redisClient != null
then: "删除客户端"
def old = repository.remove("test");
expect: "删除成功"
old != null
repository.findById("test") == null

}

def initRedissonClientSuccess(String clientId, int database) {
return repository.getRedissonClient(clientId, database) != null;
}

def "测试自定义序列化"() {
def bucket = repository.getRedissonClient("test", 0)
.getBucket("test-data", repository.getCodec("test", "test-data"));

given: "从自定义的jar中初始化类并放入redis"
def testBean = repository.classLoader.loadClass("org.hswebframework.redis.manager.beans.TestBean");
testBean.with {
id: "test"
name: "test"
age: 20
}
bucket.set(testBean)
expect: "设置成功"
bucket.get() != null
bucket.get()==testBean

}

def "测试初始化redis客户端"() {
given: "初始化客户端"
initRedissonClientSuccess(clientId, database) == success
where: "初始化结果"
clientId | database | success
"test" | 0 | true
"test" | 1 | true
"test" | 2 | true
"test" | 3 | true
"test" | 4 | true
"test" | 5 | true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CodecClassLoaderTest extends Specification {

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

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

0 comments on commit 0b155aa

Please sign in to comment.