Monredis is a thin wrapper around ioredis providing improved cluster support and sensible defaults for both single-node and cluster redis instances.
The module exports a factory function, use it to get a ioredis redis client
const Redis = require('monredis')
// Single node redis instance
const redisClient = Redis('redis://:authpwd@host:port')
// Redis cluster with keys prefix
const redisClusterClient = Redis(
'redis://:authpwd@host:port',
true,
{keyPrefix: 'myprefix'}
)
See simple.js
file inside example
folder for a more complete example.
The factory functions takes 4 arguments:
host
: string or array, required. The redis url to connect to. Can be an array of multiple urls if connecting to a cluster, so that if some nodes are down it will connect to the next one in the array for the initial connection. NOTE: You don't have to pass all the cluster nodes here when connecting to a cluster! ioredis will automatically find and connect to all nodes as soon as it connects to one; the nodes here are only the candidates for the initial connection. One or two are enough unless you have a very unstable cluster.cluster
: boolean, defaultfalse
- passtrue
to connect to redis clusternodeOptions
: object, optional - ioredis redis optionsclusterOptions
: object, optional - ioredis cluster options, minus theredisOptions
key, which we take from the previousnodeOptions
parameter.
You can check the default values for nodeOptions
and clusterOptions
in the
main source file and in the
ioredis'documentation.
The values you provide will overwrite the defaults, so you
can change a single key or override everything.
One particularly useful param in nodeOptions
is the keyPrefix
one:
if supplied, all keys in the commands executed by the client will be
automatically and transparently prefixed with the value. For example:
const Monredis = require('monredis')
const redis = Monredis('redis://localhost:6379', false, {keyPrefix: 'dev:'})
// Will actually create and read 'dev:mymapkey'
redisClient.set('mykey', 'value')
redisClient.get('mykey')
This is great for namespacing of keys in shared redis instances, or you can
also use this feature to force all your keys to end up in a single redis
cluster node by providing a prefix between {}
brackets.