Code Monkey home page Code Monkey logo

Triggers and Functions

GitHub Builds CircleCI Documentation

A Redis module that allows running a JS functions inside the Redis processes. The JS code is execute use V8 JS engine.

Notice, Triggers and Functions is still under active development and not yet GA. The API might (and probably will) change at the final GA version.

Run Using Docker

docker run -p 6379:6379 redislabs/redisgears:edge

How do I Redis?

Learn for free at Redis University

Build faster with the Redis Launchpad

Try the Redis Cloud

Dive in developer tutorials

Join the Redis community

Work at Redis

Build

With Docker

Run the following on the main directory:

> docker build -t redisgears2 .

Then run the built image:

> docker run -p 6379:6379 redisgears2

From Source

See the build page for more information.

Getting started

Run JS code

The API exposed by the module is very similar to the way Redis Functions work. Let's write a simple hello world function that returns the string hello world:

#!js name=lib api_version=1.0

redis.registerFunction('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 in the file lib.js, we can register our function in Redis using the TFUNCTION LOAD command:

> redis-cli -x TFUNCTION LOAD < ./lib.js
OK

And now we can execute our function using TFCALL command, the command gets the library name and the function name . separated:

> redis-cli TFCALL lib.hello_world 0
"hello_world"

Notice that the TFCALL command arguments are very close to the Redis FCALL command arguments. The only difference is that, for Triggers and Functions, the command also requires the library name. The 0 represents the number of keys that will follow, which in our case is 0.

Calling Redis Commands Inside Triggers and Functions

It is possible to call Redis commands inside Triggers and Functions. The function gets as the first argument a client object that allows interaction with Redis using the call function. The following example executes a simple PING command and returns the result:

#!js name=lib api_version=1.0

redis.registerFunction('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 TFUNCTION LOAD < ./lib.js
(error) Library lib already exists

We get the error because the library with the same name already exists, we can use the REPLACE argument to replace the library with the new code:

> redis-cli -x TFUNCTION LOAD UPGRADE < ./lib.js
OK

And now we can invoke my_ping using TFCALL :

> redis-cli TFCALL lib.my_ping 0
"PONG"

What's next?

redisgears's Projects

redisgears doesnโ€™t have any public repositories yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.