redis実際のゲームでデータをキャッシュする実戦

1518 ワード

1)redisサーバ
var redis = require("redis");

var client = null;

exports.connect = function (config) {
    client = redis.createClient({
        host: config.host,
        port: config.port,
        db: config.db,
    });
    console.log("cityRedis is running");
    client.on("error", function (err) {
        console.log(err);
    });
};

exports.setResources = function (key, resourcesInfo, callback) { //      
    if (client === null) {
        console.log("client is null from cityRedis");
        return;
    }

    client.hmset(key, resourcesInfo, function (err) {
        if (err) {
            console.log(err);
            callback(false);
        } else {
            callback(true);
        }
    });

    client.hgetall(key, function (err, obj) {
        console.log("type:", obj.type);
    });
};

2)使用
var key = "t_resources" + "_" + userid + "_" + rows[i].type; //       
                cityRedis.setResources(key, rows[i], function (flag) {
                    if (flag) {
                        console.log("flag is  true");
                    }
                })

まとめ:
redis:   
      ,         :

  key(t_resources_391_food) -->

  value-->
  {
    id:397,
    userid: 391,
    type: food,
    count:0.7,
    allCount:225,
    addRate:5.5,
    updateTime:1515140858,
    nowCount:225
  }

      :           (uid     ),  :  obj