zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    oracle用户被锁死
    windows远程桌面智能调整大小
    批量ping测试脚本
    信息的组织和提取方法
    BeautifulSoup
    requests模块学习
    Telerik Fiddler 应用方法
    js 时间格式换成 把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd)记录
    vuedraggable 拖拽 应用 不同列表之间的拖拽
    vue项目图片上传 vant van-uploader 图片上传
  • 原文地址:https://www.cnblogs.com/cbugs/p/11505281.html
Copyright © 2011-2022 走看看