• 使用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);
        }
    });
  • 相关阅读:
    最精简的django程序
    spring+mongo
    从零搭建mongo分片集群的简洁方法
    java对redis的基本操作
    awk输出指定列
    sed输出指定行
    Bash的循环结构(for和while)
    用ffmpeg切割音频文件
    Python判断字符串是否全是字母或数字
    Python函数: any()和all()的用法
  • 原文地址:https://www.cnblogs.com/alonecat06/p/6351493.html
Copyright © 2020-2023  润新知