CommandReader

Run RedisGears functions on command.

The CommandReader allows you to run RedisGears functions on command when you:

  1. Pass the CommandReader to the GearsBuilder.CreateGearsBuilder() function in your Java code.

  2. Call the register() function.

  3. Run RG.JEXECUTE to register your code.

  4. Use RG.TRIGGER to run your code on command:

    RG.TRIGGER <Trigger name> [arg1 arg2 ...]
    

Parameters

Name Type Description
trigger string The command name that triggers the registered RedisGears functions to run

Output records

Outputs a record with the command trigger's name and arguments.

Example

The following example shows how to create a custom command to update an item's stock. It also adds a timestamp to track when the last restock occurred.

Add a hash to the database that represents an inventory item:

redis> HSET inventory🎧1 color "blue" stock 5 price 30.00
(integer) 3

Example code:

// Create the reader that will pass data to the pipe
CommandReader reader = new CommandReader();
// Set the name of the custom command
reader.setTrigger("Restock");
        
// Create the data pipe builder
GearsBuilder.CreateGearsBuilder(reader).map(r-> {
    // Parse the command arguments to get the key name and new stock value
    String itemKey = new String((byte[]) r[1], StandardCharsets.UTF_8);
    String newStock = new String((byte[]) r[2], StandardCharsets.UTF_8);
        	
    // Update the item's stock and add a timestamp
    GearsBuilder.execute("HSET", itemKey , "stock", newStock,
        			"last_restocked", Long.toString(System.currentTimeMillis()));
        	
    return "OK restocked " + itemKey;
}).register();

After you register the previous code with the RG.JEXECUTE command, run RG.TRIGGER to test it:

redis> RG.TRIGGER Restock inventory🎧1 20
1) "OK restocked inventory🎧1"

The item now has the updated value for stock and a last_restocked timestamp:

redis> HGETALL inventory🎧1
1) "color"
2) "blue"
3) "stock"
4) "20"
5) "price"
6) "30.00"
7) "last_restocked"
8) "1643232394078"
RATE THIS PAGE
Back to top ↑