cache.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var logging = require("./logging");
  2. var node_redis = require("redis");
  3. var config = require("../config");
  4. var redis = null;
  5. // sets up redis connection
  6. // flushes redis when using ephemeral storage (e.g. Heroku)
  7. function connect_redis() {
  8. logging.log("connecting to redis...");
  9. redis = node_redis.createClient(config.redis);
  10. redis.on("ready", function() {
  11. logging.log("Redis connection established.");
  12. if (config.caching.ephemeral) {
  13. logging.log("Storage is ephemeral, flushing redis");
  14. redis.flushall();
  15. }
  16. });
  17. redis.on("error", function(err) {
  18. logging.error(err);
  19. });
  20. redis.on("end", function() {
  21. logging.warn("Redis connection lost!");
  22. });
  23. }
  24. var exp = {};
  25. // returns the redis instance
  26. exp.get_redis = function() {
  27. return redis;
  28. };
  29. // set model type to value of *slim*
  30. exp.set_slim = function(rid, userId, slim, callback) {
  31. logging.debug(rid, "setting slim for", userId, "to " + slim);
  32. // store userId in lower case if not null
  33. userId = userId && userId.toLowerCase();
  34. redis.hmset(userId, ["a", Number(slim)], callback);
  35. };
  36. // sets the timestamp for +userId+
  37. // if +temp+ is true, the timestamp is set so that the record will be outdated after 60 seconds
  38. // these 60 seconds match the duration of Mojang's rate limit ban
  39. // callback: error
  40. exp.update_timestamp = function(rid, userId, temp, callback) {
  41. logging.debug(rid, "updating cache timestamp (" + temp + ")");
  42. var sub = temp ? config.caching.local - 60 : 0;
  43. var time = Date.now() - sub;
  44. // store userId in lower case if not null
  45. userId = userId && userId.toLowerCase();
  46. redis.hmset(userId, "t", time, function(err) {
  47. callback(err);
  48. });
  49. };
  50. // create the key +userId+, store +skin_hash+, +cape_hash+, +slim+ and current time
  51. // if +skin_hash+ or +cape_hash+ are undefined, they aren't stored
  52. // this is useful to store cape and skin at separate times, without overwriting the other
  53. // +slim+ can be true (alex) or false (steve)
  54. // +callback+ contans error
  55. exp.save_hash = function(rid, userId, skin_hash, cape_hash, slim, callback) {
  56. logging.debug(rid, "caching skin:" + skin_hash + " cape:" + cape_hash + " slim:" + slim);
  57. // store shorter null value instead of "null" string
  58. skin_hash = skin_hash === null ? "" : skin_hash;
  59. cape_hash = cape_hash === null ? "" : cape_hash;
  60. // store userId in lower case if not null
  61. userId = userId && userId.toLowerCase();
  62. var args = [];
  63. if (cape_hash !== undefined) {
  64. args.push("c", cape_hash);
  65. }
  66. if (skin_hash !== undefined) {
  67. args.push("s", skin_hash);
  68. }
  69. if (slim !== undefined) {
  70. args.push("a", Number(!!slim));
  71. }
  72. args.push("t", Date.now());
  73. redis.hmset(userId, args, function(err) {
  74. callback(err);
  75. });
  76. };
  77. // removes the hash for +userId+ from the cache
  78. exp.remove_hash = function(rid, userId) {
  79. logging.debug(rid, "deleting hash from cache");
  80. redis.del(userId.toLowerCase(), "h", "t");
  81. };
  82. // get a details object for +userId+
  83. // {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512}
  84. // callback: error, details
  85. // details is null when userId not cached
  86. exp.get_details = function(userId, callback) {
  87. // get userId in lower case if not null
  88. userId = userId && userId.toLowerCase();
  89. redis.hgetall(userId, function(err, data) {
  90. var details = null;
  91. if (data) {
  92. details = {
  93. skin: data.s === "" ? null : data.s,
  94. cape: data.c === "" ? null : data.c,
  95. slim: data.a === "1",
  96. time: Number(data.t)
  97. };
  98. }
  99. callback(err, details);
  100. });
  101. };
  102. connect_redis();
  103. module.exports = exp;