Redjs is a Redis-like in-memory data store. It can be used with Ioredis, NodeRedis and by other applications (nodejs or not).
Very often, a Nodejs application uses 'cluster' module, and is running on several processes (or several servers). Then you need to share data and send messages (pub/sub) between processes.
Redis is a good solution to do that, but sometime you want to provide a standalone application without dependencies. You can then embed Redjs server in your nodejs application (or in a dedicated nodejs application) and use it like Redis.
Notes:
- The purpose of this module is not to compete with Redis (the performance of Redjs is about 2 to 3 times less than Redis, and there is no replication or cluster) but to provide a shared memory and a pub-sub system between nodejs processes, using "standard" client modules like ioredis, node_redis etc.
- Redjs implements Redis protocol and behaves like Redis: so it can be used by any application.
- All operations are performed in-memory, on master process.
- Persistence is not yet implemented
$ npm install ctoesca/redjs
/* create RedjsServer */
const RedjsServer = require('Redjs')
new RedjsServer().start(6379)
if (cluster.isMaster){
/* MASTER PROCESS */
const numProcesses = 2
for (var i=0; i<numProcesses; i++)
cluster.fork();
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died code=`+code);
cluster.fork();
})
/* create RedjsServer */
const RedjsServer = require('Redjs')
new RedjsServer().start(6379)
}else{
/*
WORKER PROCESS
*/
}
Using Redjs server with your favorite client library (Ioredis in this example)
const Redis = require('ioredis');
var redis = new Redis();
redis.set('foo', 'bar');
redis.get('foo', function (err, result) {
console.log(result);
});
// Or using a promise if the last argument isn't a function
redis.get('foo').then(function (result) {
console.log(result);
});
// Arguments to commands are flattened, so the following are the same:
redis.sadd('set', 1, 3, 5, 7);
redis.sadd('set', [1, 3, 5, 7]);
Of course, you can connect to redjs with redis-cli program (provided with redis):
redis-cli -p 6379
127.0.0.1:6379> hset hash1 var1 foo
(integer) 1
127.0.0.1:6379> hget hash1 var1
"foo"
127.0.0.1:6379> hgetall hash1
1) "var1"
2) "foo"
- subscribe
- unsubscribe
- publish
- psubscribe
- punsubscribe
- lpush
- rpush
- llen
- rpop
- lpop
- lindex
- lset
- linsert
- lrange
- lset
- hkeys
- hlen
- hget
- hset
- hmget
- hmset
- hdel
- hexists
- hgetall
- hincrby
- hincrbyfloat
- hsetnx
- hstrlen
- hvals
- del
- exists
- keys
- sadd
- srem
- smembers
- spop
- sismember
- sunion
- scard
- srandmember
- get
- set
- incr
- strlen
- monitor
- flushdb
- flushall
- info
- time
- ping
- echo
- quit
- multi
- exec
- discard