Redis filters
The following filter types are supported for Redis connections:
Request filters
JavaScript request filter - Redis: generic request filter that executes arbitrary JavaScript code
Request logger - Redis: logs requests to stdout, to a file, or to a logger
Response filters
JavaScript response filter - Redis: generic response filter that executes arbitrary JavaScript code
Response logger - Redis: logs responses to stdout, to a file, or to a logger
Duplex filters
JavaScript duplex filter - Redis: generic duplex filter that executes arbitrary JavaScript code for requests and responses
Scripting environment
When a filter is invoked, the following variables will be defined, in addition to the usual JavaScript environment:
context.packet: the current Redis object request or response, always one of the 5 Redis types. For requests, this is always an Array.
context.connectionContext.lastRequest: in a response filter, an array containing the request for which this is the response
context.redisUtil: see below
The context.redisUtil object
This object has the following methods:
object createObject(string type, object value)
Creates a new Redis object that can be added to or inserted into a request or a response.
The type parameter must be one of the Redis types, namely: "SimpleString", "BulkString", "Error", "Integer", "Array".
The value parameter is used to initialize the value of the object. It should be a string for "SimpleString", "BulkString" and "Error", and a number for "Integer". For "Array", it can be null for an empty array, or a JavaScript array, or a string, or a number.
The same effect can be accomplished with the following methods:
object createSimpleString(object value)
Creates a new Redis SimpleString from the given object. SimpleString are rarely used.
object createBulkString(object value)
Creates a new Redis BulkString from the given object. BulkStrings are the most common type of strings in Redis.
object createInteger(object value)
Creates a new Redis Integer from the given object, which should be a number or a string representation of a number. If the number has decimals, it is rounded down.
object createArray()
object createArray(object value)
Creates a new Redis Array. If a value is passed in, it is added to the new array, unless it is itself an array, in which case its contents will be added to the new array.
object createError(object value)
Creates a new Redis Error object with the given value, which should be a string.
object convertToRedis(object obj)
Converts the given JavaScript object to its Redis equivalent. If obj is:
null, then null is returned
a string, then a RedisBulkString is returned
a number, then a RedisInteger is returned (rounded down if the number has decimals)
a boolean, then a RedisBulkString is returned with value "true" or "false"
an object, then a RedisArray is returned in the format of a HashMap for Redis, namely an array with key and value pairs, where the keys are RedisBulkStrings, and the values are converted to Redis objects
an array, then a RedisArray is returned, containing whatever was in the given array, converted to Redis objects
Examples
Create an array and put it in the current request or response:
let newArray = context.redisUtil.createObject("Array", [789, "Hello"]);
newArray.push("This");
newArray.push(123);
// newArray is now: [789, "Hello", "This", 123]
context.packet[2] = newArray;
Create a complex object:
let response = context.redisUtil.convertToRedis({
name: "Jane Doe",
age: 54,
scores: [78, 96, 32],
address: {
street: "23 Main St",
city: "Oakland",
state: "CA"
}});
This will return the following Redis Array, in which a string is a BulkString, a number is an Integer, and an array is an Array:
["name", "Jane Doe", "age", 54, "score", [78, 96, 32], address, ["street", "23 Main St", "city", "Oakland", "state", "CA"]]