cache.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var logging = require("./logging");
  2. var node_redis = require("redis");
  3. var config = require("../config");
  4. var url = require("url");
  5. var redis = null;
  6. // sets up redis connection
  7. // flushes redis when using ephemeral storage (e.g. Heroku)
  8. function connect_redis() {
  9. logging.log("connecting to redis...");
  10. // parse redis env
  11. var redis_env = process.env.REDISCLOUD_URL || process.env.REDIS_URL;
  12. var redis_url = redis_env ? url.parse(redis_env) : {};
  13. redis_url.port = redis_url.port || 6379;
  14. redis_url.hostname = redis_url.hostname || "localhost";
  15. // connect to redis
  16. redis = node_redis.createClient(redis_url.port, redis_url.hostname);
  17. if (redis_url.auth) {
  18. redis.auth(redis_url.auth.split(":")[1]);
  19. }
  20. redis.on("ready", function() {
  21. logging.log("Redis connection established.");
  22. if (process.env.EPHEMERAL_STORAGE) {
  23. logging.log("Storage is ephemeral, flushing redis");
  24. redis.flushall();
  25. }
  26. });
  27. redis.on("error", function(err) {
  28. logging.error(err);
  29. });
  30. redis.on("end", function() {
  31. logging.warn("Redis connection lost!");
  32. });
  33. }
  34. var exp = {};
  35. // returns the redis instance
  36. exp.get_redis = function() {
  37. return redis;
  38. };
  39. // updates the redis instance's server_info object
  40. // callback: error, info object
  41. exp.info = function(callback) {
  42. redis.info(function(err, res) {
  43. // parse the info command and store it in redis.server_info
  44. // this code block was taken from mranney/node_redis#on_info_cmd
  45. // http://git.io/LBUNbg
  46. var lines = res.toString().split("\r\n");
  47. var obj = {};
  48. lines.forEach(function(line) {
  49. var parts = line.split(":");
  50. if (parts[1]) {
  51. obj[parts[0]] = parts[1];
  52. }
  53. });
  54. obj.versions = [];
  55. if (obj.redis_version) {
  56. obj.redis_version.split(".").forEach(function(num) {
  57. obj.versions.push(+num);
  58. });
  59. }
  60. redis.server_info = obj;
  61. callback(err, redis.server_info);
  62. });
  63. };
  64. // set model type to value of *slim*
  65. exp.set_slim = function(rid, userId, slim, callback) {
  66. logging.debug(rid, "setting slim for", userId, "to " + slim);
  67. // store userId in lower case if not null
  68. userId = userId && userId.toLowerCase();
  69. redis.hmset(userId, ["a", Number(slim)], callback);
  70. };
  71. // sets the timestamp for +userId+
  72. // if +temp+ is true, the timestamp is set so that the record will be outdated after 60 seconds
  73. // these 60 seconds match the duration of Mojang's rate limit ban
  74. // callback: error
  75. exp.update_timestamp = function(rid, userId, temp, callback) {
  76. logging.debug(rid, "updating cache timestamp (" + temp + ")");
  77. var sub = temp ? config.caching.local - 60 : 0;
  78. var time = Date.now() - sub;
  79. // store userId in lower case if not null
  80. userId = userId && userId.toLowerCase();
  81. redis.hmset(userId, "t", time, function(err) {
  82. callback(err);
  83. });
  84. };
  85. // create the key +userId+, store +skin_hash+, +cape_hash+, +slim+ and current time
  86. // if +skin_hash+ or +cape_hash+ are undefined, they aren't stored
  87. // this is useful to store cape and skin at separate times, without overwriting the other
  88. // +slim+ can be true (alex) or false (steve)
  89. // +callback+ contans error
  90. exp.save_hash = function(rid, userId, skin_hash, cape_hash, slim, callback) {
  91. logging.debug(rid, "caching skin:" + skin_hash + " cape:" + cape_hash + " slim:" + slim);
  92. // store shorter null value instead of "null" string
  93. skin_hash = skin_hash === null ? "" : skin_hash;
  94. cape_hash = cape_hash === null ? "" : cape_hash;
  95. // store userId in lower case if not null
  96. userId = userId && userId.toLowerCase();
  97. var args = [];
  98. if (cape_hash !== undefined) {
  99. args.push("c", cape_hash);
  100. }
  101. if (skin_hash !== undefined) {
  102. args.push("s", skin_hash);
  103. }
  104. if (slim !== undefined) {
  105. args.push("a", Number(!!slim));
  106. }
  107. args.push("t", Date.now());
  108. redis.hmset(userId, args, function(err) {
  109. callback(err);
  110. });
  111. };
  112. // removes the hash for +userId+ from the cache
  113. exp.remove_hash = function(rid, userId) {
  114. logging.debug(rid, "deleting hash from cache");
  115. redis.del(userId.toLowerCase(), "h", "t");
  116. };
  117. // get a details object for +userId+
  118. // {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512}
  119. // callback: error, details
  120. // details is null when userId not cached
  121. exp.get_details = function(userId, callback) {
  122. // get userId in lower case if not null
  123. userId = userId && userId.toLowerCase();
  124. redis.hgetall(userId, function(err, data) {
  125. var details = null;
  126. if (data) {
  127. details = {
  128. skin: data.s === "" ? null : data.s,
  129. cape: data.c === "" ? null : data.c,
  130. slim: data.a === "1",
  131. time: Number(data.t)
  132. };
  133. }
  134. callback(err, details);
  135. });
  136. };
  137. connect_redis();
  138. module.exports = exp;