LettuceMod is a Java client for Redis Modules based on Lettuce. It supports the following modules in standalone or cluster configurations:
<dependency>
<groupId>com.redis</groupId>
<artifactId>lettucemod</artifactId>
<version>3.5.0</version>
</dependency>
dependencies {
implementation 'com.redis:lettucemod:3.5.0'
}
<dependency>
<groupId>com.redis</groupId>
<artifactId>spring-lettucemod</artifactId>
<version>3.5.0</version>
</dependency>
dependencies {
implementation 'com.redis:spring-lettucemod:3.5.0'
}
For early-access releases use the following repository:
<repositories>
<repository>
<id>oss.sonatype.org-snapshot</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
repositories {
maven {
url "https://s01.oss.sonatype.org/content/repositories/snapshots/"
}
}
RedisModulesClient client = RedisModulesClient.create("redis://localhost:6379"); // (1)
StatefulRedisModulesConnection<String, String> connection = client.connect(); // (2)
RedisModulesCommands<String, String> commands = connection.sync(); // (3)
-
Create a modules client
-
Connect to Redis server
List<RedisURI> uris = Arrays.asList(RedisURI.create("node1", 6379), RedisURI.create("node2", 6379)); // (1)
RedisModulesClusterClient client = RedisModulesClusterClient.create(uris); // (2)
StatefulRedisModulesClusterConnection<String, String> connection = client.connect(); // (3)
RedisModulesAdvancedClusterCommands<String, String> commands = connection.sync(); // (4)
-
Create list of cluster node URIs
-
Create a cluster client
-
Connect to Redis servers
-
Use the sync, async, or reactive API
GenericObjectPoolConfig<StatefulRedisModulesConnection<String, String>> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(4); // (1)
// ...
GenericObjectPool<StatefulRedisModulesConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(client::connect, config); // (2)
-
Create a pool configuration
-
Create the connection pool
@Component
public class MyComponent {
@Autowired
StatefulRedisModulesConnection<String, String> connection;
// ...
}
RedisGearsCommands<String, String> gears = connection.sync(); // (1)
gears.rgPyexecute("GearsBuilder().run('person:*')"); // (2)
-
Use sync, async, or reactive RedisGears commands
-
Execute a RedisGears Python function
RedisJSONCommands<String, String> json = connection.sync(); // (1)
json.jsonSet("arr", ".", "[1,2,3]"); // (2)
-
Use sync, async, or reactive RedisJSON commands
-
Set a JSON value
RediSearchCommands<String, String> search = connection.sync(); // (1)
search.ftCreate("beers", Field.text("name").build(), Field.numeric("ibu").build()); // (2)
SearchResults<String, String> results = search.ftSearch("beers", "chou*"); // (3)
-
Use sync, async, or reactive RediSearch commands
-
Create an index
-
Search the index
RedisTimeSeriesCommands<String, String> ts = connection.sync(); // (1)
ts.tsAdd("temp:3:11", Sample.of(1548149181, 30)); // (2)
-
Use sync, async, or reactive RedisTimeSeries commands
-
Append a new sample to the series
RedisModulesAsyncCommands<String, String> commands = connection.async();
commands.setAutoFlushCommands(false); // (1)
List<RedisFuture<?>> futures = new ArrayList<>(); // (2)
for (MyEntity element : entities()) {
futures.add(commands.ftSugadd("names", Suggestion.of(element.getName(), element.getScore())));
}
commands.flushCommands(); // (3)
boolean result = LettuceFutures.awaitAll(5, TimeUnit.SECONDS,
futures.toArray(new RedisFuture[0])); // (4)
connection.close(); // (5)
-
Disable auto-flushing
-
Perform a series of independent calls
-
Write all commands to the transport layer
-
Synchronization example: Wait until all futures complete
-
Later
GenericObjectPoolConfig<StatefulRedisModulesConnection<String, String>> config = new GenericObjectPoolConfig<>(); // (1)
config.setMaxTotal(16);
// ...
GenericObjectPool<StatefulRedisModulesConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(client::connect, config); // (2)
try (StatefulRedisModulesConnection<String, String> connection = pool.borrowObject()) { // (3)
RedisModulesAsyncCommands<String, String> commands = connection.async(); // (4)
// ...
} catch (Exception e) {
log.error("Could not get a connection from the pool", e);
}
-
Create a pool configuration
-
Create the connection pool
-
Get connection from pool. Try-with automatically closes connection which returns it to pool
-
Use sync, async, or reactive commands