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

Commit 5cdd905

Browse files
committed
#3 增加自定义序列化功能
1 parent ccdab84 commit 5cdd905

File tree

10 files changed

+139
-14
lines changed

10 files changed

+139
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ hs_err_pid*
2525
/upload
2626
/ui/upload/
2727
build/
28+
!/lib/*.jar

.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 test
10+
- mvn -Dcodec.lib.dir=${PWD}/lib/ test
1111
after_success:
1212
- bash <(curl -s https://codecov.io/bash)
1313
cache:
2.82 KB
Binary file not shown.

pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,9 @@
203203
<executions>
204204
<execution>
205205
<goals>
206-
<goal>addSources</goal>
207206
<goal>addTestSources</goal>
208-
<goal>generateStubs</goal>
209207
<goal>compile</goal>
210-
<goal>generateTestStubs</goal>
211208
<goal>compileTests</goal>
212-
<goal>removeStubs</goal>
213-
<goal>removeTestStubs</goal>
214209
</goals>
215210
</execution>
216211
</executions>

redis-manager-core/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616
<groupId>org.redisson</groupId>
1717
<artifactId>redisson</artifactId>
1818
</dependency>
19+
20+
<dependency>
21+
<groupId>net.openhft</groupId>
22+
<artifactId>zero-allocation-hashing</artifactId>
23+
<version>0.8</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>de.ruedigermoeller</groupId>
28+
<artifactId>fst</artifactId>
29+
<version>2.55</version>
30+
</dependency>
31+
32+
<!-- https://mvnrepository.com/artifact/com.esotericsoftware/kryo -->
33+
<dependency>
34+
<groupId>com.esotericsoftware</groupId>
35+
<artifactId>kryo</artifactId>
36+
<version>4.0.2</version>
37+
</dependency>
38+
1939
<dependency>
2040
<groupId>io.vavr</groupId>
2141
<artifactId>vavr</artifactId>

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

+24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
package org.hswebframework.redis.manager.codec;
22

3+
import io.vavr.API;
4+
import io.vavr.CheckedFunction1;
5+
6+
import java.io.File;
37
import java.net.URL;
48
import java.net.URLClassLoader;
9+
import java.util.stream.Stream;
510

611
public class CodecClassLoader extends URLClassLoader {
712

13+
public CodecClassLoader(File... urls) {
14+
super(Stream.of(urls)
15+
.flatMap(API.unchecked(file -> {
16+
if (file.isDirectory()) {
17+
File[] children = file.listFiles();
18+
return children == null ? Stream.empty() : Stream.of(children);
19+
}
20+
return Stream.of(file);
21+
}))
22+
.map(API.unchecked(file -> file.toURI().toURL()))
23+
.toArray(URL[]::new));
24+
}
25+
26+
public CodecClassLoader(String... urls) {
27+
super(Stream.of(urls)
28+
.map(API.<String, URL>unchecked(URL::new))
29+
.toArray(URL[]::new));
30+
}
31+
832
public CodecClassLoader(URL... urls) {
933
super(urls);
1034
}

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,27 @@
77
import org.redisson.codec.SerializationCodec;
88

99
public enum CodecType {
10-
string(){
10+
string() {
1111
@Override
1212
public Codec createCodec(ClassLoader classLoader) {
1313
return StringCodec.INSTANCE;
1414
}
1515
},
16-
jdk(){
16+
jdk() {
1717
@Override
1818
public Codec createCodec(ClassLoader classLoader) {
1919
return new SerializationCodec(classLoader);
2020
}
2121
},
22-
fst(){
22+
fst() {
2323
@Override
2424
public Codec createCodec(ClassLoader classLoader) {
25-
26-
return null;//new FstCodec(classLoader);
25+
return new FstCodec(classLoader);
2726
}
2827
},
29-
kryo(){
28+
kryo() {
3029
@Override
3130
public Codec createCodec(ClassLoader classLoader) {
32-
33-
3431
return new KryoCodec(classLoader);
3532
}
3633
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.hswebframework.redis.manager.codec
2+
3+
import spock.lang.Specification
4+
5+
/**
6+
* @author zhouhao
7+
* @since 1.0.0
8+
*/
9+
class CodecClassLoaderTest extends Specification {
10+
11+
def "测试自定义类加载"() {
12+
given:
13+
def classLoader = new CodecClassLoader(new File(System.getProperty("codec.lib.dir", "./lib")))
14+
15+
and:
16+
def testBean = classLoader.loadClass("org.hswebframework.redis.manager.beans.TestBean");
17+
18+
expect:
19+
testBean != null
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.hswebframework.redis.manager.codec
2+
3+
import io.netty.buffer.ByteBuf
4+
import org.redisson.client.codec.Codec
5+
import org.redisson.client.handler.State
6+
import spock.lang.Shared
7+
import spock.lang.Specification
8+
9+
/**
10+
* @author zhouhao
11+
* @since 1.0.0
12+
*/
13+
class CodecTypeTest extends Specification {
14+
15+
@Shared
16+
def string = CodecType.string.createCodec(this.class.getClassLoader());
17+
@Shared
18+
def jdk = CodecType.jdk.createCodec(this.class.getClassLoader());
19+
@Shared
20+
def fst = CodecType.fst.createCodec(this.class.getClassLoader());
21+
@Shared
22+
def kryo = CodecType.kryo.createCodec(this.class.getClassLoader());
23+
24+
@Shared
25+
def bean = new TestClass(id: "test", name: "测试", age: 18, enabled: true);
26+
27+
def doEncodeDecode(Codec c, Object o) {
28+
ByteBuf buf = c.getValueEncoder().encode(o)
29+
def decode = c.getValueDecoder().decode(buf, new State(false))
30+
return decode == o
31+
}
32+
33+
def "测试序列化"() {
34+
given: "准备执行"
35+
doEncodeDecode(codec, data) == success
36+
where: "序列化,反序列成功"
37+
codec | data | success
38+
string | bean | true
39+
fst | bean | true
40+
jdk | bean | true
41+
kryo | bean | true
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.hswebframework.redis.manager.codec;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* @author zhouhao
11+
* @since 1.0
12+
*/
13+
@EqualsAndHashCode
14+
@Getter
15+
@Setter
16+
public class TestClass implements Serializable {
17+
private String id;
18+
19+
private String name;
20+
21+
private int age;
22+
23+
private boolean enabled;
24+
}

0 commit comments

Comments
 (0)