Skip to content

Redis module

Francesco Saverio Castellano edited this page Mar 20, 2020 · 26 revisions

Redis module is based on libhiredis c library.
This module is NOT thread safe, therefore each worker must establish its own connection to the redis server by calling the connect function.

Redis.connect(string host, integer port)
Connects to the redis server.  
 
Redis.close()
Closes the connection to the redis server.  
 
Redis.command(string command)
Executes the specified redis command and returns a reply object. The returned object has a type property that represents the type of reply returned by redis. The possible values for type are represented by the following constants:

  • Redis.REPLY_STRING: String reply. In this case the reply object has a string property that carries the returned string value
  • Redis.REPLY_ARRAY: Array reply. In this case the reply object has a elements property that carries the returned array. Each item in the elements array is an object that has the same structure as the parent reply object (e.g. if an array of strings is returned then each item in the array is an object with two properties: type set to Redis.REPLY_STRING and string set to the value of the element)
  • Redis.REPLY_INTEGER: Integer reply. In this case the reply object has a integer property that carries the returned integer value
  • Redis.REPLY_NIL: Null reply. This is the reply type when the requested item has not been found
  • Redis.REPLY_STATUS: Commands like set that do not return a value return a reply of this type. In this case the reply objact contains a string attribute that is set to OK is the command was succesful
  • Redis.REPLY_ERROR: Indicates that there has been an error executing the command

 
Redis.commandArgv(string cmd, string arg1, string arg2, ..., string argN)
Similar to Redis.command but in this case the name of the redis command and its arguments must be provided as separated parameters when calling the function.
This function takes care of encoding every string parameter in the proper way according to the Redis protocol so that if the string contains spaces then these are not treated as separators of Redis command attributes.
This allows setting keys whose value contains one or more spaces without risking that part of the value are interpreted as command attributes.
To understand how Redis.commandArgv is useful, consider the following example code. The function storeObjectWithTTL stores an object with the specified time to live (TTL).
If storeObjectWithTTL was based on Redis.command rather than Redis.commandArgv then the serialized string that represents the object would break the set command because of the space in the name attribute. By using Redis.commandArgv the serialized string is encoded as a single command attribute which includes also the spaces in it.

function storeObjectWithTTL(obj, key, ttl) {
    var res = Redis.commandArgv('set', key, JSON.stringify(obj), 'EX', ''+ttl);
    return (res.type == Redis.REPLY_STATUS && res.string == 'OK');
}

var myFriend = {
     id: 1,
     age: 20,
     name: 'Jorge Newman'
};

storeObjectWithTTL(myFriend, 'friend:'+myFriend.id, 60);

 
 
Redis.getReply()
Gets the reply object of the next command that has been executed through a pipeline (see Redis.appendCommand).  
 
Redis.appendCommand(string command)
This function allows using pipelines which basically means sending a batch of commands to the Redis server in a single request.
When calling Redis.appendCommand the command is not executed, it is just appended to the internal output buffer. Therefore multiple subsequent calls to Redis.appendCommand can be made in order to append several commands that will be send altogether in a single request.
The commands are sent when the first call to Redis.getReply is made after the commands have been appended.
The following examples shows how to execute multiple commands in a pipeline:

//create a pipeline with 3 commands (none of the commands will be executed yet)
Redis.appendCommand("set mykey1 10");
Redis.appendCommand("set mykey2 30");
Redis.appendCommand("set mykey3 40");

//executes all 3 commands in the pipeline and returns the reply of the first one
var reply1 = Redis.getReply(); 

//returns the reply of the second command
var reply2 = Redis.getReply(); 

//returns the reply of the third command
var reply3 = Redis.getReply(); 
Clone this wiki locally