douyang2530
douyang2530
2016-08-01 06:50

NodeJS,Redis和MySQL

Please has anyone ever used NodeJS + Redis + MySQL The goal is to have NodeJS fetch data from Redis and only reach MySQL if data not found, also update MySQL back with the data.

Then how can i run relational request from Redis knowing its a NoSQL in-memory DB

I have read that Redis is better than Memcached and it seems to be faster so I wanted to try it out but it seems all tutorial i can lay my hands on are just PHP based.

Currently I use NodeJs + MySQL as below (example) :

var d = new Date();
var date = d.getFullYear() + '-' + paddnum(Math.round(d.getMonth() + 1)) + '-' + paddnum(d.getDate()) + ' ' + paddnum(d.getHours()) + ':' + paddnum(d.getMinutes()) + ':' + paddnum(d.getSeconds());

var query = `INSERT INTO comments (COMMENT, POSTID)
                        VALUES ('{"author":"` + data.userid + `", "comment": "` + data.comment + `", "time":"` + date + `"}', ` + data.postid + `)
                        ON DUPLICATE KEY UPDATE
                            COMMENT = CONCAT(COMMENT, ',{"author":"` + data.userid + `", "comment": "` + data.comment + `", "time":"` + date + `"}'),
                            POSTID = ` + data.postid;
//console.log(query);
connection.query(query,
    function(err, results) {
        if (err) {
            return err;
        }
        return results;
    }
);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doukui4836 doukui4836 5年前

    Im not sure what you really need, but based on question you need code and module that will work between nodejs code and MySQL db. You can use this module for redis operation git://github.com/NodeRedis/node_redis.git

    Now all what you need is connect to redis, fill the data in and then on user request check redis->if found return data if not do db queries. Or vice versa.

    Hope this helps.

    点赞 评论 复制链接分享

相关推荐