Skip to content

Commit 258dcfc

Browse files
author
chenby
committed
function example
1 parent 3bdefd9 commit 258dcfc

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

examples/com/moilioncircle/examples/migration/MigrationExample.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.moilioncircle.examples.migration;
1818

1919
import static redis.clients.jedis.Protocol.Command.AUTH;
20-
import static redis.clients.jedis.Protocol.Command.RESTORE;
2120
import static redis.clients.jedis.Protocol.Command.SELECT;
2221
import static redis.clients.jedis.Protocol.toByteArray;
2322

@@ -40,13 +39,16 @@
4039
import com.moilioncircle.redis.replicator.event.EventListener;
4140
import com.moilioncircle.redis.replicator.rdb.datatype.DB;
4241
import com.moilioncircle.redis.replicator.rdb.dump.DumpRdbVisitor;
42+
import com.moilioncircle.redis.replicator.rdb.dump.datatype.DumpFunction;
4343
import com.moilioncircle.redis.replicator.rdb.dump.datatype.DumpKeyValuePair;
4444
import com.moilioncircle.redis.replicator.util.Strings;
4545

4646
import redis.clients.jedis.DefaultJedisClientConfig;
4747
import redis.clients.jedis.HostAndPort;
4848
import redis.clients.jedis.Jedis;
4949
import redis.clients.jedis.Protocol;
50+
import redis.clients.jedis.args.FunctionRestorePolicy;
51+
import redis.clients.jedis.params.RestoreParams;
5052

5153
/**
5254
* @author Leon Chen
@@ -84,6 +86,15 @@ public static void sync(String sourceUri, String targetUri) throws IOException,
8486
r.addEventListener(new EventListener() {
8587
@Override
8688
public void onEvent(Replicator replicator, Event event) {
89+
90+
// function since redis 7.0
91+
if (event instanceof DumpFunction) {
92+
DumpFunction dfn = (DumpFunction) event;
93+
Object r = target.restoreFunction(dfn.getSerialized(), true);
94+
System.out.println(r);
95+
}
96+
97+
// key value
8798
if (event instanceof DumpKeyValuePair) {
8899
DumpKeyValuePair dkv = (DumpKeyValuePair) event;
89100
// Step1: select db
@@ -107,6 +118,7 @@ public void onEvent(Replicator replicator, Event event) {
107118
}
108119
}
109120

121+
// incremental commands
110122
if (event instanceof DefaultCommand) {
111123
// Step3: sync aof command
112124
DefaultCommand dc = (DefaultCommand) event;
@@ -255,9 +267,17 @@ public Object send(final byte[] cmd, final byte[]... args) {
255267

256268
public Object restore(byte[] key, long expired, byte[] dumped, boolean replace) {
257269
if (!replace) {
258-
return send(RESTORE, key, toByteArray(expired), dumped);
270+
return jedis.restore(key, expired, dumped);
271+
} else {
272+
return jedis.restore(key, expired, dumped, RestoreParams.restoreParams().replace());
273+
}
274+
}
275+
276+
public Object restoreFunction(byte[] dumped, boolean replace) {
277+
if (!replace) {
278+
return jedis.functionRestore(dumped, FunctionRestorePolicy.APPEND);
259279
} else {
260-
return send(RESTORE, key, toByteArray(expired), dumped, "REPLACE".getBytes());
280+
return jedis.functionRestore(dumped, FunctionRestorePolicy.REPLACE);
261281
}
262282
}
263283

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<dependency>
128128
<groupId>redis.clients</groupId>
129129
<artifactId>jedis</artifactId>
130-
<version>4.4.2</version>
130+
<version>5.1.0</version>
131131
<scope>test</scope>
132132
</dependency>
133133
</dependencies>

0 commit comments

Comments
 (0)