forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ExamplesApp to test get/set/info/ping
Signed-off-by: Andrew Carbonetto <[email protected]>
- Loading branch information
1 parent
926d04e
commit a7bc168
Showing
8 changed files
with
236 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
plugins { | ||
// Apply the application plugin to add support for building a CLI application in Java. | ||
id 'application' | ||
} | ||
|
||
repositories { | ||
// Use Maven Central for resolving dependencies. | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation project(':client') | ||
|
||
implementation 'redis.clients:jedis:4.4.3' | ||
implementation 'io.lettuce:lettuce-core:6.2.6.RELEASE' | ||
implementation 'commons-cli:commons-cli:1.5.0' | ||
} | ||
|
||
application { | ||
// Define the main class for the application. | ||
mainClass = 'glide.examples.ExamplesApp' | ||
applicationDefaultJvmArgs = ['-Djava.library.path=../target/release'] | ||
} |
96 changes: 96 additions & 0 deletions
96
java/examples/src/main/java/glide/examples/ExamplesApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package glide.examples; | ||
|
||
import glide.api.RedisClient; | ||
import glide.examples.clients.GlideClient; | ||
import glide.examples.clients.JedisClient; | ||
import glide.examples.clients.LettuceAsyncClient; | ||
import java.util.concurrent.ExecutionException; | ||
import redis.clients.jedis.commands.JedisCommands; | ||
|
||
public class ExamplesApp { | ||
|
||
// main application entrypoint | ||
public static void main(String[] args) { | ||
|
||
// runJedisExamples(); | ||
// runLettuceExamples(); | ||
runGlideExamples(); | ||
} | ||
|
||
private static void runJedisExamples() { | ||
ConnectionSettings settings = new ConnectionSettings("localhost", 6379, false, false); | ||
|
||
System.out.println("Connecting to Redis using Jedis-client"); | ||
JedisCommands jedisCommands = JedisClient.connectToRedis(settings); | ||
System.out.println("Jedis SET(myKey, myValue): " + jedisCommands.set("myKey", "myValue")); | ||
System.out.println("Jedis GET(myKey): " + jedisCommands.get("myKey")); | ||
System.out.println("Jedis GET(invalid): " + jedisCommands.get("invalid")); | ||
} | ||
|
||
private static void runLettuceExamples() { | ||
|
||
ConnectionSettings settings = new ConnectionSettings("localhost", 6379, false, false); | ||
|
||
System.out.println("Connecting to Redis using Lettuce-client"); | ||
LettuceAsyncClient lettuceAsyncClient = new LettuceAsyncClient(settings); | ||
|
||
try { | ||
System.out.println( | ||
"Lettuce SET(myKey, myValue): " | ||
+ lettuceAsyncClient.asyncCommands.set("myKey", "myValue").get()); | ||
System.out.println( | ||
"Lettuce GET(myKey): " + lettuceAsyncClient.asyncCommands.get("myKey").get()); | ||
System.out.println( | ||
"Lettuce GET(invalid): " + lettuceAsyncClient.asyncCommands.get("invalid").get()); | ||
|
||
} catch (InterruptedException | ExecutionException e) { | ||
System.out.println("Lettuce example failed with an exception: "); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static void runGlideExamples() { | ||
ConnectionSettings settings = new ConnectionSettings("localhost", 6379, false, false); | ||
|
||
try { | ||
RedisClient client = GlideClient.connectToGlide(settings); | ||
|
||
System.out.println("Glide PING: " + client.ping().get()); | ||
System.out.println("Glide PING(custom): " + client.ping("found you!").get()); | ||
|
||
// panic | ||
// System.out.println("Glide INFO(): " + client.info().get()); | ||
|
||
System.out.println("Glide SET(myKey, myValue): " + client.set("myKey", "myValue").get()); | ||
System.out.println("Glide GET(myKey): " + client.get("myKey").get()); | ||
System.out.println("Glide SET(myKey, yourValue): " + client.set("myKey", "yourValue").get()); | ||
System.out.println("Glide GET(myKey): " + client.get("myKey").get()); | ||
System.out.println("Glide GET(invalid): " + client.get("invalid").get()); | ||
|
||
Object customGetMyKey = client.customCommand(new String[] {"get", "myKey"}).get(); | ||
System.out.println("Glide CUSTOM_COMMAND(get, myKey): " + customGetMyKey); | ||
|
||
Object customGetInvalid = client.customCommand(new String[] {"get", "invalid"}).get(); | ||
System.out.println("Glide CUSTOM_COMMAND(get, invalid): " + customGetInvalid); | ||
|
||
} catch (ExecutionException | InterruptedException e) { | ||
System.out.println("Glide example failed with an exception: "); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** Redis-client settings */ | ||
public static class ConnectionSettings { | ||
public final String host; | ||
public final int port; | ||
public final boolean useSsl; | ||
public final boolean clusterMode; | ||
|
||
public ConnectionSettings(String host, int port, boolean useSsl, boolean clusterMode) { | ||
this.host = host; | ||
this.port = port; | ||
this.useSsl = useSsl; | ||
this.clusterMode = clusterMode; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
java/examples/src/main/java/glide/examples/clients/GlideClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package glide.examples.clients; | ||
|
||
import glide.api.RedisClient; | ||
import glide.api.models.configuration.NodeAddress; | ||
import glide.api.models.configuration.RedisClientConfiguration; | ||
import glide.examples.ExamplesApp; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** Connect to Jedis client. See: https://github.com/redis/jedis */ | ||
public class GlideClient { | ||
public static RedisClient connectToGlide(ExamplesApp.ConnectionSettings connectionSettings) | ||
throws ExecutionException, InterruptedException { | ||
if (connectionSettings.clusterMode) { | ||
throw new RuntimeException("Not implemented"); | ||
} | ||
RedisClientConfiguration config = | ||
RedisClientConfiguration.builder() | ||
.address( | ||
NodeAddress.builder() | ||
.host(connectionSettings.host) | ||
.port(connectionSettings.port) | ||
.build()) | ||
.useTLS(connectionSettings.useSsl) | ||
.build(); | ||
return RedisClient.CreateClient(config).get(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
java/examples/src/main/java/glide/examples/clients/JedisClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package glide.examples.clients; | ||
|
||
import glide.examples.ExamplesApp; | ||
import java.util.Set; | ||
import redis.clients.jedis.DefaultJedisClientConfig; | ||
import redis.clients.jedis.HostAndPort; | ||
import redis.clients.jedis.JedisCluster; | ||
import redis.clients.jedis.JedisPool; | ||
import redis.clients.jedis.commands.JedisCommands; | ||
|
||
/** Connect to Jedis client. See: https://github.com/redis/jedis */ | ||
public class JedisClient { | ||
|
||
public static JedisCommands connectToRedis(ExamplesApp.ConnectionSettings connectionSettings) { | ||
JedisCommands jedis; | ||
if (connectionSettings.clusterMode) { | ||
jedis = | ||
new JedisCluster( | ||
Set.of(new HostAndPort(connectionSettings.host, connectionSettings.port)), | ||
DefaultJedisClientConfig.builder().ssl(connectionSettings.useSsl).build()); | ||
} else { | ||
try (JedisPool pool = | ||
new JedisPool( | ||
connectionSettings.host, connectionSettings.port, connectionSettings.useSsl)) { | ||
jedis = pool.getResource(); | ||
} | ||
} | ||
return jedis; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
java/examples/src/main/java/glide/examples/clients/LettuceAsyncClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package glide.examples.clients; | ||
|
||
import glide.examples.ExamplesApp; | ||
import io.lettuce.core.AbstractRedisClient; | ||
import io.lettuce.core.RedisClient; | ||
import io.lettuce.core.RedisURI; | ||
import io.lettuce.core.api.StatefulConnection; | ||
import io.lettuce.core.api.StatefulRedisConnection; | ||
import io.lettuce.core.api.async.RedisStringAsyncCommands; | ||
import io.lettuce.core.cluster.RedisClusterClient; | ||
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; | ||
import java.time.Duration; | ||
|
||
/** Connect to lettuce client - see: https://lettuce.io/ */ | ||
public class LettuceAsyncClient { | ||
static final int ASYNC_OPERATION_TIMEOUT_SEC = 1; | ||
|
||
private AbstractRedisClient client; | ||
public RedisStringAsyncCommands<String, String> asyncCommands; | ||
private StatefulConnection<String, String> connection; | ||
|
||
public LettuceAsyncClient(ExamplesApp.ConnectionSettings connectionSettings) { | ||
RedisURI uri = | ||
RedisURI.builder() | ||
.withHost(connectionSettings.host) | ||
.withPort(connectionSettings.port) | ||
.withSsl(connectionSettings.useSsl) | ||
.build(); | ||
if (!connectionSettings.clusterMode) { | ||
client = RedisClient.create(uri); | ||
connection = ((RedisClient) client).connect(); | ||
asyncCommands = ((StatefulRedisConnection<String, String>) connection).async(); | ||
} else { | ||
client = RedisClusterClient.create(uri); | ||
connection = ((RedisClusterClient) client).connect(); | ||
asyncCommands = ((StatefulRedisClusterConnection<String, String>) connection).async(); | ||
} | ||
connection.setTimeout(Duration.ofSeconds(ASYNC_OPERATION_TIMEOUT_SEC)); | ||
} | ||
|
||
public void closeConnection() { | ||
connection.close(); | ||
client.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ rootProject.name = 'glide' | |
|
||
include 'client' | ||
include 'integTest' | ||
include 'examples' | ||
include 'benchmarks' |