10. Additional features

Rui Gu edited this page Oct 26, 2016 · 7 revisions

10.1. Operations with Redis nodes

Redisson NodesGroup object provides some control over Redis nodes.

NodesGroup nodesGroup = redisson.getNodesGroup();
nodesGroup.addConnectionListener(new ConnectionListener() {
    public void onConnect(InetSocketAddress addr) {
       // Redis server connected

    public void onDisconnect(InetSocketAddress addr) {
       // Redis server disconnected

Allows to ping single Redis server or all of them.

NodesGroup nodesGroup = redisson.getNodesGroup();
Collection<Node> allNodes = nodesGroup.getNodes();
for (Node n : allNodes) {
// or

10.2. References to Redisson objects

It's possible to use a Redisson object inside another Redisson object in any combination. In this case a special reference object will be used and handled by Redisson. Usage example:

RMap<RSet<RList>, RList<RMap>> map = redisson.getMap("myMap");
RSet<RList> set = redisson.getSet("mySet");
RList<RMap> list = redisson.getList("myList");

map.put(set, list);
// With the help of the special reference object, we can even create a circular
// reference which is impossible to achieve if we were to serialize its content

As you may have noticed there is no need to re "save/persist" the map object after its elements have changed. Because it does not contain any value but merely a reference, this makes Redisson objects behaves much more like standard Java objects. In effect, making Redis becomes part of JVM's memory rather than just a simple repository.

One Redis HASH, one Redis SET and one Redis LIST will be created in this example.

10.3. Execution batches of commands

Multiple commands can be sent together at one execution via RBatch object as one network call. By using this object you can decrease time execution of command group. In Redis this approach called Pipelining.

RBatch batch = redisson.createBatch();
batch.getMap("test").fastPutAsync("1", "2");
batch.getMap("test").fastPutAsync("2", "3");
batch.getMap("test").putAsync("2", "5");

List<?> res = batch.execute();
// or
Future<List<?>> asyncRes = batch.executeAsync();

// send commands and waits for their execution but skip any result
// or

In cluster environment it acting in map\reduce way. Aggregates commands for each node and sends them simultaneously. Result from each node will be added to common result list.

10.4. Scripting

String r = redisson.getScript().eval(Mode.READ_ONLY,
   "return redis.call('get', 'foo')", RScript.ReturnType.VALUE);

// do the same using cache
RScript s = redisson.getScript();
// load script into cache to all redis master instances
String res = s.scriptLoad("return redis.call('get', 'foo')");
// res == 282297a0228f48cd3fc6a55de6316f31422f5d17

// call script by sha digest
Future<Object> r1 = redisson.getScript().evalShaAsync(Mode.READ_ONLY,
   RScript.ReturnType.VALUE, Collections.emptyList());

10.5. Low level Redis client

Redisson uses high-perfomance async and lock-free Redis client for Java. It supports both async and sync modes. You may use it if you want to execute a command not supported by Redisson yet. Anyway, try to find your command in Redis command mapping list before you use low-level client.

RedisClient client = new RedisClient("localhost", 6379);
RedisConnection conn = client.connect();
Future<RedisConnection> connFuture = client.connectAsync();

conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
conn.async(StringCodec.INSTANCE, RedisCommands.GET, "test");


// or

// or