Skip to content

Basic usage

alopumomovia edited this page Oct 27, 2017 · 9 revisions
Example 1. Basic usage
RedisClient client = RedisClient.create("redis://localhost");          (1)

StatefulRedisConnection<String, String> connection = client.connect(); (2)

RedisCommands<String, String> commands = connection.sync();            (3)

String value = commands.get("foo");                                    (4)

...

connection.close();                                                    (5)

client.shutdown();                                                     (6)
  1. Create the RedisClient instance and provide a Redis URI pointing to localhost, Port 6379 (default port).

  2. Open a Redis Standalone connection. The endpoint is used from the initialized RedisClient

  3. Obtain the command API for synchronous execution. Lettuce supports asynchronous and reactive execution models, too.

  4. Issue a GET command to get the key foo.

  5. Close the connection when you’re done. This happens usually at the very end of your application. Connections are designed to be long-lived.

  6. Shut down the client instance to free threads and resources. This happens usually at the very end of your application.

Each Redis command is implemented by one or more methods with names identical to the lowercase Redis command name. Complex commands with multiple modifiers that change the result type include the CamelCased modifier as part of the command name, e.g. zrangebyscore and zrangebyscoreWithScores.

Redis connections are designed to be long-lived and thread-safe, and if the connection is lost will reconnect until close() is called. Pending commands that have not timed out will be (re)sent after successful reconnection.

All connections inherit a default timeout from their RedisClient and
and will throw a RedisException when non-blocking commands fail to return a result before the timeout expires. The timeout defaults to 60 seconds and may be changed in the RedisClient or for each connection. Synchronous methods will throw a RedisCommandExecutionException in case Redis responds with an error. Asynchronous connections do not throw exceptions when Redis responds with an error.

RedisURI

The RedisURI contains the host/port and can carry authentication/database details. On a successful connect you get authenticated, and the database is selected afterward. This applies
also after re-establishing a connection after a connection loss.

A Redis URI can also be created from an URI string. Supported formats are:

  • redis://[password@]host[:port][/databaseNumber] Plaintext Redis connection

  • rediss://[password@]host[:port][/databaseNumber] SSL Redis connection

  • redis-sentinel://[password@]host[:port][,host2[:port2]][/databaseNumber]#sentinelMasterId for using Redis Sentinel

  • redis-socket:///path/to/socket Unix Domain Socket connection to Redis

Exceptions

In the case of an exception/error response from Redis, you’ll receive a RedisException containing
the error message. RedisException is a RuntimeException.

Examples

Example 2. Using host and port and set the default timeout to 20 seconds
RedisClient client = RedisClient.create(RedisURI.create("localhost", 6379));
client.setDefaultTimeout(20, TimeUnit.SECONDS);

// …

client.shutdown();
Example 3. Using RedisURI
RedisURI redisUri = RedisURI.Builder.redis("localhost")
                                .withPassword("authentication")
                                .withDatabase(2)
                                .build();
RedisClient client = RedisClient.create(rediUri);

// …

client.shutdown();
Example 4. SSL RedisURI
RedisURI redisUri = RedisURI.Builder.redis("localhost")
                                .withSsl(true)
                                .withPassword("authentication")
                                .withDatabase(2)
                                .build();
RedisClient client = RedisClient.create(redisUri);

// …

client.shutdown();
Example 5. String RedisURI
RedisURI redisUri = RedisURI.create("redis://authentication@localhost/2");
RedisClient client = RedisClient.create(rediUri);

// …

client.shutdown();
Clone this wiki locally