首页 > 代码库 > 使用node_redis进行redis数据库crud操作
使用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操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。