Learning to use Pomelo to develop a game server, run into node. JS Operations Redis, record
Suppose the scenario is to manipulate the data of a user table
Introducing the Node_redis library to create a client
var redis = require ("Redis"); var client = Redis.createclient ();
Create User Data
varPlayers =NewArray ();p Layers.push ({"id": "Name": "Test Player11", "VIP": "$": $, "PSD": "password1234"});p Layers.push ({"id": 932, "name": "Test Player12", "VIP": One, "RMB": $, "PSD": "password1234"});p Layers.push ({"id": 936, "name": "Test Player13", "VIP": Ten, "RMB": $, "PSD": "password1234"});p Layers.push ({"id": 626, "name": "Test Player14", "VIP": 9, "$": $, "PSD": "password1234"});varPlayer;client.get (' Uidx ',function(Err, uidx) { for(vari = 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) {}); }});
Search by player number
varplayerID = 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); } } }) }});
Update data based on player number
var = playerID; var player = {"id": $, "name": "Angle", "VIP": Bayi, "RMB": "$", "PSD": "Waitforu"};client.hget ( function(err, uidx) { if (err) { console.log (err); return ; } Else { client.hmset ("User:" + uidx, player, redis.print); });
Delete data based on player number
var playerid =client.hget (function(err, uidx) { if (ERR) { console.log (err); return ; } Else { Client.del ("User:" + uidx); Client.del (' userId: ' + playerID); }});
Using Node_redis for Redis database crud operations