cache.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // set model type to value of *slim*
  40. exp.set_slim = function(rid, userId, slim, callback) {
  41. logging.debug(rid, "setting slim for", userId, "to " + slim);
  42. // store userId in lower case if not null
  43. userId = userId && userId.toLowerCase();
  44. redis.hmset(userId, ["a", Number(slim)], callback);
  45. };
  46. // sets the timestamp for +userId+
  47. // if +temp+ is true, the timestamp is set so that the record will be outdated after 60 seconds
  48. // these 60 seconds match the duration of Mojang's rate limit ban
  49. // callback: error
  50. exp.update_timestamp = function(rid, userId, temp, callback) {
  51. logging.debug(rid, "updating cache timestamp (" + temp + ")");
  52. var sub = temp ? config.caching.local - 60 : 0;
  53. var time = Date.now() - sub;
  54. // store userId in lower case if not null
  55. userId = userId && userId.toLowerCase();
  56. redis.hmset(userId, "t", time, function(err) {
  57. callback(err);
  58. });
  59. };
  60. // create the key +userId+, store +skin_hash+, +cape_hash+, +slim+ and current time
  61. // if +skin_hash+ or +cape_hash+ are undefined, they aren't stored
  62. // this is useful to store cape and skin at separate times, without overwriting the other
  63. // +slim+ can be true (alex) or false (steve)
  64. // +callback+ contans error
  65. exp.save_hash = function(rid, userId, skin_hash, cape_hash, slim, callback) {
  66. logging.debug(rid, "caching skin:" + skin_hash + " cape:" + cape_hash + " slim:" + slim);
  67. // store shorter null value instead of "null" string
  68. skin_hash = skin_hash === null ? "" : skin_hash;
  69. cape_hash = cape_hash === null ? "" : cape_hash;
  70. // store userId in lower case if not null
  71. userId = userId && userId.toLowerCase();
  72. var args = [];
  73. if (cape_hash !== undefined) {
  74. args.push("c", cape_hash);
  75. }
  76. if (skin_hash !== undefined) {
  77. args.push("s", skin_hash);
  78. }
  79. if (slim !== undefined) {
  80. args.push("a", Number(!!slim));
  81. }
  82. args.push("t", Date.now());
  83. redis.hmset(userId, args, function(err) {
  84. callback(err);
  85. });
  86. };
  87. // removes the hash for +userId+ from the cache
  88. exp.remove_hash = function(rid, userId) {
  89. logging.debug(rid, "deleting hash from cache");
  90. redis.del(userId.toLowerCase(), "h", "t");
  91. };
  92. // get a details object for +userId+
  93. // {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512}
  94. // callback: error, details
  95. // details is null when userId not cached
  96. exp.get_details = function(userId, callback) {
  97. // get userId in lower case if not null
  98. userId = userId && userId.toLowerCase();
  99. redis.hgetall(userId, function(err, data) {
  100. var details = null;
  101. if (data) {
  102. details = {
  103. skin: data.s === "" ? null : data.s,
  104. cape: data.c === "" ? null : data.c,
  105. slim: data.a === "1",
  106. time: Number(data.t)
  107. };
  108. }
  109. callback(err, details);
  110. });
  111. };
  112. connect_redis();
  113. module.exports = exp;