forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJedisScriptingExample.java
More file actions
196 lines (164 loc) · 8.03 KB
/
JedisScriptingExample.java
File metadata and controls
196 lines (164 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.examples;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.FlushMode;
/**
* Example demonstrating Jedis compatibility layer scripting features. Shows usage of EVAL, EVALSHA,
* SCRIPT management, FCALL, and FUNCTION commands.
*/
public class JedisScriptingExample {
public static void main(String[] args) {
// Connect to Valkey server
try (Jedis jedis = new Jedis("localhost", 6379)) {
System.out.println("Connected to Valkey server");
// Lua scripting examples
demonstrateLuaScripting(jedis);
// Function examples (requires Valkey 7.0+)
try {
demonstrateFunctions(jedis);
} catch (Exception e) {
System.out.println(
"Skipping function examples - requires Valkey 7.0+: " + e.getMessage());
}
}
}
/** Demonstrates Lua scripting with EVAL, EVALSHA, and SCRIPT commands. */
private static void demonstrateLuaScripting(Jedis jedis) {
System.out.println("\n=== Lua Scripting Examples ===");
// 1. Simple EVAL
System.out.println("\n1. Simple EVAL:");
Object result = jedis.eval("return 'Hello from Lua!'");
System.out.println("Result: " + result);
// 2. EVAL with keys and arguments
System.out.println("\n2. EVAL with keys and arguments:");
jedis.set("mykey", "myvalue");
Object value = jedis.eval("return redis.call('GET', KEYS[1])", 1, "mykey");
System.out.println("Value from script: " + value);
// 3. EVAL with multiple keys and arguments
System.out.println("\n3. EVAL with multiple keys and arguments:");
String script = "return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}";
List<String> keys = Arrays.asList("key1", "key2");
List<String> args = Arrays.asList("arg1", "arg2");
Object multiResult = jedis.eval(script, keys, args);
if (multiResult instanceof Object[]) {
System.out.println("Results: " + Arrays.toString((Object[]) multiResult));
}
// 4. SCRIPT LOAD and EVALSHA
System.out.println("\n4. SCRIPT LOAD and EVALSHA:");
String luaScript = "return ARGV[1] .. ' World!'";
String sha1 = jedis.scriptLoad(luaScript);
System.out.println("Script SHA1: " + sha1);
// Check if script exists
List<Boolean> exists = jedis.scriptExists(sha1);
System.out.println("Script exists: " + exists.get(0));
// Execute by SHA1
Object shaResult =
jedis.evalsha(sha1, Collections.emptyList(), Collections.singletonList("Hello"));
System.out.println("EVALSHA result: " + shaResult);
// 5. Complex script with Redis operations
System.out.println("\n5. Complex script with Redis operations:");
jedis.set("counter", "0");
String counterScript =
"redis.call('INCR', KEYS[1])\n"
+ "redis.call('INCR', KEYS[1])\n"
+ "return redis.call('GET', KEYS[1])";
Object counterResult = jedis.eval(counterScript, 1, "counter");
System.out.println("Counter after two increments: " + counterResult);
// 6. SCRIPT management
System.out.println("\n6. SCRIPT management:");
String anotherScript = "return 'test'";
String sha2 = jedis.scriptLoad(anotherScript);
// Check multiple scripts
List<Boolean> multiExists = jedis.scriptExists(sha1, sha2);
System.out.println("First script exists: " + multiExists.get(0));
System.out.println("Second script exists: " + multiExists.get(1));
// Flush scripts
System.out.println("Flushing script cache...");
String flushResult = jedis.scriptFlush(FlushMode.SYNC);
System.out.println("Flush result: " + flushResult);
// Verify scripts are flushed
List<Boolean> afterFlush = jedis.scriptExists(sha1);
System.out.println("Script exists after flush: " + afterFlush.get(0));
}
/**
* Demonstrates Valkey Functions with FCALL, FUNCTION LOAD, and FUNCTION management commands.
* Requires Valkey 7.0 or higher.
*/
private static void demonstrateFunctions(Jedis jedis) {
System.out.println("\n=== Valkey Functions Examples ===");
// 1. Load a simple function
System.out.println("\n1. Loading a simple function:");
String simpleLib =
"#!lua name=simplelib\n"
+ "redis.register_function('greet', function(keys, args)\n"
+ " return 'Hello, ' .. args[1]\n"
+ "end)";
String libName = jedis.functionLoad(simpleLib);
System.out.println("Loaded library: " + libName);
// Call the function
Object greeting =
jedis.fcall("greet", Collections.emptyList(), Collections.singletonList("World"));
System.out.println("Function result: " + greeting);
// 2. Load a function that uses keys and arguments
System.out.println("\n2. Function with keys and arguments:");
String dataLib =
"#!lua name=datalib\n"
+ "redis.register_function('setget', function(keys, args)\n"
+ " redis.call('SET', keys[1], args[1])\n"
+ " return redis.call('GET', keys[1])\n"
+ "end)";
jedis.functionLoad(dataLib);
Object setGetResult =
jedis.fcall(
"setget",
Collections.singletonList("mykey"),
Collections.singletonList("myvalue"));
System.out.println("Set and get result: " + setGetResult);
// 3. List functions
System.out.println("\n3. Listing functions:");
List<Object> functions = jedis.functionList();
System.out.println("Number of libraries loaded: " + functions.size());
// List functions with code
List<Object> functionsWithCode = jedis.functionListWithCode();
System.out.println("Functions with code: " + functionsWithCode.size() + " libraries");
// 4. Function DUMP and RESTORE
System.out.println("\n4. Function DUMP and RESTORE:");
byte[] dump = jedis.functionDump();
System.out.println("Dumped " + dump.length + " bytes");
// Flush functions
jedis.functionFlush();
System.out.println("Functions flushed");
// Restore from dump
String restoreResult = jedis.functionRestore(dump);
System.out.println("Restore result: " + restoreResult);
// Verify restoration
Object verifyResult =
jedis.fcall("greet", Collections.emptyList(), Collections.singletonList("Again"));
System.out.println("Function after restore: " + verifyResult);
// 5. Function stats
System.out.println("\n5. Function statistics:");
Object stats = jedis.functionStats();
System.out.println("Function stats retrieved: " + (stats != null));
// 6. Replace a function
System.out.println("\n6. Replacing a function:");
String replacedLib =
"#!lua name=simplelib\n"
+ "redis.register_function('greet', function(keys, args)\n"
+ " return 'Hi, ' .. args[1] .. '!'\n"
+ "end)";
String replacedName = jedis.functionLoadReplace(replacedLib);
System.out.println("Replaced library: " + replacedName);
Object newGreeting =
jedis.fcall(
"greet", Collections.emptyList(), Collections.singletonList("Replacement"));
System.out.println("New function result: " + newGreeting);
// Clean up
System.out.println("\n7. Cleaning up:");
jedis.functionDelete("simplelib");
jedis.functionDelete("datalib");
System.out.println("Libraries deleted");
}
}