redis blog

IBM 看到的blog如何 存储在redis种

 var ArticleHelper = function () {
     this.ArticleIDSet = "AIDSet"; // 存放文章 ID 的集合 this.ArticleIDPrefix = "Article"; // 文章 KEY 前缀
     this.CommentIDPrefix = "Comment"; // 评论 KEY 前缀 
};

    ArticleHelper.prototype.getArticleIDs = function(){ 
        var that = this; 
        var promise = ArticleMySQLHelper.getIDs().then(function (artis) { 
            if(artis.length == 0 ) {
                return null; 
           } 
           var _ = []; 
           artis.forEach(
               function(arti) {
                   _.push(redis.zaddAsync(that.ArticleIDSet, [1, arti.ID]));
               }
           ); 
           return Q.all(_); 
        });
        return promise;
    };
//取数据
ArticleHelper.prototype.getArticle = function(id){
        var that = this; 
        var promise = redis.hgetallAsync(that.ArticleIDPrefix+id).then(function(res) {
            if (res == null) {
            return ArticleMySQLHelper.getOne(id).then(function(article) {
                    return redis.hmsetAsync(that.ArticleIDPrefix+id, article). then(function(res) { 
                        return   Q.resolve(article); 
                    });
                }); 
        } else {
                return Q.resolve(res);
        }
    });
    return promise; 
}

//获取评论
ArticleHelper.prototype.getComment = function(id) { 
    var that = this; 
    var promise = redis.zrangeAsync(that.CommentIDPrefix+id, [0, -1]).then(function(res) { 
        if (res == null) { 
            var p = ArticleMySQLHelper.getComments(id).then(function(cmmts) { 
                var _ = []; cmmts.forEach(function(cmmt) {
                     _.push(redis.zaddAsync(that.CommentIDPrefix+id,[cmmt.index, cmmt.body])); });
                      return Q.all(_); 
                    });
                    return p; 
        } else {
             return  Q.resolve(res); 
        } 
    })
     return promise; 
    }
//添加评论
ArticleHelper.prototype.addComment = function(id, comment) { 
    var that = this; 
    var promise = ArticleMySQLHelper.saveComment(id, comment).then(function(res){ 
        return redis.expireAsync(that.ArticleIDPrefix+id, [0]); }); 
        return promise; 
    };
    
//评论过期
ArticleHelper.prototype.getArticleExpire = function(id) {
    var that = this; 
    var promise = redis.hgetallAsync(that.ArticleIDPrefix+id).then(function(res) { 
        if (res == null) {
            return ArticleMySQLHelper.getOne(id).then(function(article) { 
                return redis.hmsetAsync(that.ArticleIDPrefix+id, article).then(function(res) {
                    return  redis.expireAsync(that.ArticleIDPrefix+id, 3600*2); 
                }).then(function(){ return Q.resolve(article); 
                });
             }); 
        } else {
             return Q.resolve(res); 
        } 
    });
     return promise; 
    }  
####这是用到的redis 命令
zadd
zrange
hgetall
hget
hmset
hmget
expire
https://www.ibm.com/developerworks/cn/opensource/os-cn-nodejs-redis/index.html
https://redis.io/commands/hmset
原文地址:https://www.cnblogs.com/cbugs/p/11505281.html