AsyncRedisClient is a common client interface. It has two implementations AsyncRedisConnection
and ShardedAsyncRedisClient.
AsyncRedisConnection is the central class providing the connection to the Redis Server via an Asynchronous socket API. The
Asynchronous connection is abstracted by the interface AsyncSocketTransport.
Currently AsyncJavaSocketTransport using the Java 7 NIO based Asynchronous channel is the only
implementation of the interface which is why you need Java 7.
ShardedAsyncRedisClient is an implementation of AsyncRedisClient providing Sharding of Keys across 2 or more
AsyncRedisConnection's. The Hash of the Key is computed and based on the Hash the command is passed on to one of the connections
based on a Consistent Hashing algorithm. Commands with multiple keys are not allowed unless all of the keys map to the same
connection. So you will get an Exception if you submit a command like MSETNX. MSET and MGET are commands with multiple keys which
are exceptions to this rule. For these commands the keys are distributed based on their hashes and the results re-assembled as
part of the API.
RedisCommandInfo is the DTO (Data Transfer Object) for a
RedisCommand. It is a holder for the RedisCommand and its parameters, the DataHandler to be used for serializing and de-serializing
data and the Result. The getResult method returns a
String in case of a String result or Redis error. It returns the de-serialized result in case of a bulk response and an array of
de-serialized Objects in case of a multi-bulk response. The response is an array of RedisCommandInfo for each command in the
transaction in case of an EXEC command. getResult returns null in case of a null bulk or multi-bulk response and also in case of an
error executing the command such as a network error in which case the getRunStatus method returns the appropriate status other than
SUCCESS. null is also returned in case EXEC fails because of a change in a watched key.
There are different variations of the submitCommand method in AsyncRedisClient based on the following parameters:
-
RedisCommandInfo parameter: You pass a RedisCommandInfo Object containing the command with the DataHandler along
with other arguments.
-
RedisCommand with arguments: This is a convenience wrapper on the previous set of commands where the RedisCommandInfo
constructor arguments are flattened along with the rest of the parameters. These method implementations construct a
RedisCommandInfo object and call one of the previous methods.
-
RedisCommand[] array: These commands are executed together in the pipeline. So it can be used for a MULTI-EXEC set of
commands not requiring a WATCH on one or more keys.
-
Without a completionHandler: These commands return a Future Object on which a get method can be called to get the RedisCommandInfo
or RedisCommandInfo[] array containing the results.
-
With a completionHandler: These are async commands that call the completionHandler with the RedisCommandInfo or RedisCommandInfo[] array
containing the results.
-
With a null completionHandler: You can pass a null completionHandler when you want to submit the command and are not interested in the
results. This Send and Forget call is slightly better than using a command that returns a Future and ignoring it because the
Future object does not need to be created and notified. The sendCommand methods are convenience wrappers providing the same
functionality.
-
With a DataHandler: The values in a Redis Command are converted to bytes when sending them and converted back to Object
when processing Return values using a DataHandler. You can pass a Data Handler in case you want to override the default Data
Handler.
The return value if any of all submit command(s) calls is a Future Object of RedisCommandInfo or RedisCommandInfo[]; The value returned by the
Future is the same as what was passed with the result set in the CommandInfo(s).
The submitCommand calls work as follows.
First the command is serialized and submitted to a request Q.
An async NIO based request Q listener de-queues the commands from the request Q one by one, sends them to the REDIS server and moves the command to the response Q.
An async NIO based response Q listener de-queues the commands from the response Q one by one, reads and parses the corresponding response from the REDIS server and sends back the response.
The de-serialization happens on the first call to getResult on the returned CommandInfo after which the de-serialized result is
saved and returned on subsequent calls.
AsyncObjectPool is a fixed size Object pool with Async
borrow functionality used by aredis to provide a connection pool for Redis Transactions using WATCH, MULTI and EXEC commands.
RedisSubscription provides an API for subscribing to channels
using redis's SUBSCRIBE or PSUBSCRIBE commands.
AsyncRedisFactory is a factory class for getting one of the
above services for a given redis server. It returns the same Object for the same connection and is the preferred way of using these
services. The getClient method
returns an AsyncRedisClient for a single or Multiple Servers. The implementation of AsyncRedisClient returned is an instance
AsyncRedisConnection if only one server is passed and an instance of ShardedAsyncRedisClient if a comma separated list of servers
is passed. The
getConnectionPool
method returns an instance of AsyncObjectPool and the
getSubscription
method returns an instance of RedisSubscription.
Script is an immutable Object holding a Redis Lua script and its digest. It
should be used for the EVALCHECK command and is recommended
as a parameter for other script commands like EVALSHA though you won't usually need to use them if you use the EVALCHECK pseudo
command. A script object is created using the
getInstance method which returns the same
script object for identical scripts. Each script is also assigned a unique index starting with 0 upon creation. The index is used
internally to maintain the status flags for each Redis Server to indicate if the script has been verified on that server.