douyang2530 2016-08-01 06:50
浏览 214
已采纳

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 2016-08-01 10:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?