user1816608
user1816608

Reputation:

Storing my Logs to REDIS

I'm a little confused.
I want to filter my records based on SL and HOST.
And I want to get records in date range.
I guess I couldn't understand the use of SADD and ZADD.

My code likes this :

console.log("sl:"+req.params.sl+" | host:"+req.params.host+" | source:"+req.params.source+" | date:"+req.body.date+" | title:"+req.body.title+" | details:"+req.body.details);
var key="log"+req.body.date;
redis_client.hmset(key,{"date":req.body.date,"title":req.body.title,"details":req.body.details,"sl":req.params.sl,"source":req.params.source,"host":req.params.host});
redis_client.sadd(key,"host",req.params.host,redis.print);
redis_client.sadd(key,"sl",req.params.host,redis.print);
redis_client.zadd(key,"date",req.body.date,redis.print);

Upvotes: 0

Views: 397

Answers (1)

r043v
r043v

Reputation: 1869

a key can only get one type, here you define key as hash, set and zset

try construct the key name using your filter data

as you need get records in date range, using a single list key by day and host could be pertinent

var entry = {
    date:req.body.date,
    title:req.body.title,
    details:req.body.details,
    sl:req.params.sl,
    source:req.params.source,
    host:req.params.host
}

var key = 'mylog:'+req.params.host+':'+currentDay;

redis_client.rpush(key,entry);
redis_client.expire(key,30*24*60*60); // expire in one month

for catch all entry of a particular day for any host just use lrange command

function getLog(host,day,callback){
    redis_client.lrange('mylog:'+host+':'+day,0,-1,callback);
}

and you can use keys command to filter keys using wildcards (not do that in production keys is slow!)

function getLogDay(day,callback){
    redis_client.keys('mylog:*:'+day,function(keys){
        var multi = redis_client.multi(); // check your redis client real syntax
        // loop on result keys to get them all
        for(var n=0;n<keys.length;n++)
            multi.lrange(keys[n],0,-1);
        multi.exec(callback);
    });
}

Upvotes: 1

Related Questions