• 使用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);
        }
    });
  • 相关阅读:
    javascript进阶,从表达式到引用类型,变量监听
    nodejs和es6,es5等关系
    前后端分离工程带来的问题
    vue难点解析
    angular框架及其UI使用
    Javascript入门和TypeScrip入门
    从熟悉项目到开发项目
    昌平某公司入职一周感想
    css和前端UI框架设计ElementUI
    2020新征程
  • 原文地址:https://www.cnblogs.com/alonecat06/p/6351493.html
Copyright © 2020-2023  润新知