Saturday, 27 March 2021

node-redis with ZADD , ZRANGE , ZREMRANGEBYSCORE , ZREVRANGEBYSCORE

 


Node-Redis with ZADD , ZRANGE , ZREMRANGEBYSCORE , ZREVRANGEBYSCORE with
Limit and offset usecase in Chat Application.

Commands with Optional and Keyword arguments
This applies to anything that uses an optional [WITHSCORES] or
[LIMIT offset count] in the redis.io/commands documentation.

Example:

var args = [ 'myzset', 1, 'one', 2, 'two', 3, 'three', 99, 'ninety-nine' ];
client.zadd(args, function (err, response) {
if (err) throw err;
console.log('added '+response+' items.');

var args1 = [ 'myzset', '+inf', '-inf' ];
client.zrevrangebyscore(args1, function (err, response) {
if (err) throw err;
console.log('example1', response);
// write your code here
});

var max = 3, min = 1, offset = 1, count = 2;
var args2 = [ 'myzset', max, min, 'WITHSCORES', 'LIMIT', offset, count ];
client.zrevrangebyscore(args2, function (err, response) {
if (err) throw err;
console.log('example2', response);
// write your code here
});
});

// set using node-redis client

let from_user = data.from_user;
let to_user = data.to_user;
let chat_uuid = new Date().getTime();
let chat_key = "chat:from:"+from_user+":to:"+to_user;
redisUtil.redisCli.zadd(chat_key,chat_uuid,JSON.stringify(data),
function(chat_save_error,chat_save_data){
console.log(chat_save_error);
});

// get using node-redis client
let chat_key = "chat:from:"+inputs.from_user+":to:"+inputs.to_user;
redisUtil.redisCli.zrange(chat_key,0,-1,'withscores',function(err,result){

});

// Delete Chat From Radis In Beetween Ids
ZREMRANGEBYSCORE chat:from:ADMIN2:to:ADMIN (1616750982327 1616750995307

// Get All Chat From Redis WITHSCORES
ZRANGE chat:from:ADMIN:to:ADMIN2 0 -1 WITHSCORES

// Get Last 5 Chat From Redis
ZREVRANGEBYSCORE chat:from:ADMIN:to:ADMIN2 +inf -inf LIMIT 0 5


No comments:

Post a Comment