CommandOverrider
Override a Redis command.
The CommandOverrider
allows you to override and customize Redis commands.
- Pass the
CommandOverrider
to theGearsBuilder.CreateGearsBuilder()
function in your Java code. - Call the
register()
function. - Run
RG.JEXECUTE
to register your code.
Note:
If you register code that uses CommandOverrider
, its reader
value is "CommandReader"
when you run the RG.DUMPREGISTRATIONS
command, not "CommandOverrider"
.Parameters
Name | Type | Description |
---|---|---|
command | string | The command to override |
prefix | string | Only override the command for keys that start with this string |
Output records
Outputs a record with the command's name and arguments.
Example
The following example shows how to override the HSET
command so that it also adds a last_modified
timestamp for "user:" hashes.
Add some user data as a hash:
redis> HSET user:1 name "morgan" posts 201
(integer) 2
Example code:
// Create the reader that will pass data to the pipe
CommandOverrider overrider = new CommandOverrider();
// Override the HSET command
overrider.setCommand("HSET");
// Only override HSET for keys that start with "user:"
overrider.setPrefix("user:");
// Create the data pipe builder
GearsBuilder.CreateGearsBuilder(overrider).map(r-> {
// Extract the key from the command arguments
String keyName = new String((byte[]) r[1], StandardCharsets.UTF_8);
// Add a last_modified timestamp to the user's profile
GearsBuilder.execute("HSET", keyName, "last_modified", Long.toString(System.currentTimeMillis()));
// Get the original HSET arguments
ArrayList<String> commandArray = new ArrayList<String>();
for (int i=1; i < r.length; i++) {
commandArray.add(new String((byte[]) r[i], StandardCharsets.UTF_8));
}
// Run the original HSET command
GearsBuilder.callNext(commandArray.toArray(new String[0]));
return "OK";
}).register();
After you register the previous code with the RG.JEXECUTE
command, try to update the user's data with HSET
to test it:
redis> HSET user:1 posts 234
"OK"
Now the user's profile should have the updated value for posts
and a last_modified
timestamp:
redis> HGETALL user:1
1) "name"
2) "morgan"
3) "posts"
4) "234"
5) "last_modified"
6) "1643237927663"