使用node_redis进行redis数据库crud操作
Posted 读来du往de猫的笔记本
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用node_redis进行redis数据库crud操作相关的知识,希望对你有一定的参考价值。
正在学习使用pomelo开发游戏服务器,碰到node.js操作redis,记录一下
假设应用场景是操作一个用户表的数据
引入node_redis库,创建客户端
var redis = require("redis"); var client = redis.createClient();
创建用户数据
var players = new Array(); players.push({"id":49, "name":"test player11","vip":12,"rmb":23,"psd":"password1234"}); players.push({"id":932, "name":"test player12","vip":11,"rmb":23,"psd":"password1234"}); players.push({"id":936, "name":"test player13","vip":10,"rmb":23,"psd":"password1234"}); players.push({"id":626, "name":"test player14","vip":9,"rmb":23,"psd":"password1234"}); var player; client.get(‘uidx‘, function (err, uidx) { for(var i = 0; i < players.length; ++i) { client.incr(‘uidx‘); uidx++; player = players[i]; client.hmset("user:"+ uidx, player , function (err) { }); // client.hmset("user:"+ uidx // ,‘userId‘, player.id // ,‘username‘,player.name // ,‘password‘, player.psd // , function (err) { // }); client.hmset("userId:"+ player.id ,‘uidx‘, uidx , function (err) { }); } });
根据玩家编号查询
var playerID = 49; client.hget(‘userId:‘+ playerID, ‘uidx‘, function(err, uidxQuery){ if (err) { console.log(err); return; } else { //client.hget(‘user:‘+ uidxQuery, ‘username‘, function (err, username) { client.hgetall(‘user:‘+ uidxQuery, function (err1, user) { if (err1) { console.log(err); return; } else { if (user) { console.log(user.username); } else { console.log("no user id is " + playerID); } } }) } });
根据玩家编号更新数据
var playerID = 49; var player = {"id":49, "name":"angle","vip":81,"rmb":41,"psd":"waitforu"}; client.hget(‘userId:‘+ playerID, ‘uidx‘, function(err, uidx) { if (err) { console.log(err); return; } else { client.hmset("user:"+ uidx, player, redis.print); } });
根据玩家编号删除数据
var playerID = 49; client.hget(‘userId:‘+ playerID, ‘uidx‘, function(err, uidx) { if (err) { console.log(err); return; } else { client.del("user:"+ uidx); client.del(‘userId:‘+ playerID); } });
以上是关于使用node_redis进行redis数据库crud操作的主要内容,如果未能解决你的问题,请参考以下文章
node_redis 获取 zrange withscores
windows 分辨率任意定制;windows resolution custom - Custom Resolution Utility (CRU)