RedisGears-2.0
A Redis module that allows running a JS functions inside the Redis processes. The JS code is execute use V8 JS engine.
Notice, RedisGears 2.0 is still under active development and not yet GA, the API might (and probably) change at the final GA version.
Run Using Docker
docker run -p 6379:6379 redislabs/redisgears:edgeBuild
With Docker
Run the following on the main directory:
> docker build -t redisgears2 .Then run the built image:
> docker run -p 6379:6379 redisgears2From Source
See the build page for more information.
Getting started
Run JS code
The API expose by the module is very similar to the way Redis Functions is working. Lets write a simple hello world RedisGears function that return the string hello world:
#!js name=lib
redis.register_function('hello_world', function(){
return 'hello_world';
});The first line indicates the engine to use (js) and the library name (lib). The rest is the library code.
Assuming we put the following code on a file lib.js, we can register our function on RedisGears using RG.FUNCTION LOAD command:
> redis-cli -x RG.FUNCTION LOAD < ./lib.js
OKAnd now we can execute our function using RG.FCALL command, the command gets the library name and the function name:
> redis-cli RG.FCALL lib hello_world 0
"hello_world"Notice that RG.FCALL command arguments is very close to Redis FCALL command, the only difference is that on RedisGears the command also gets the library name. The 0 represent the number of keys that will follow (which in our case is 0).
Calling Redis Commands Inside our Gears Function
It is possible to call Redis commands inside our gears function. The function gets as first argument a client object that allows interaction with Redis using call function. The following example executes a simple PING command and return the result:
#!js name=lib
redis.register_function('my_ping', function(client){
return client.call('ping');
});If we will try to send it to our running Redis instance, we will get the following error:
> redis-cli -x RG.FUNCTION LOAD < ./lib.js
(error) Library lib already existsWe get the error because the library with the same name already exists, we can use the UPGRADE argument to upgrade the library with the new code:
> redis-cli -x RG.FUNCTION LOAD UPGRADE < ./lib.js
OKAnd now we can invoke my_ping using RG.FCALL :
> redis-cli RG.FCALL lib my_ping 0
"PONG"