cache.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. var logging = require("./logging");
  2. var node_redis = require("redis");
  3. var config = require("./config");
  4. var path = require("path");
  5. var url = require("url");
  6. var fs = require("fs");
  7. var redis = null;
  8. // sets up redis connection
  9. // flushes redis when running on heroku (files aren't kept between pushes)
  10. function connect_redis() {
  11. logging.log("connecting to redis...");
  12. // parse redis env
  13. var redis_env = (process.env.REDISCLOUD_URL || process.env.REDIS_URL);
  14. var redis_url = redis_env ? url.parse(redis_env) : {};
  15. redis_url.port = redis_url.port || 6379;
  16. redis_url.hostname = redis_url.hostname || "localhost";
  17. // connect to redis
  18. redis = node_redis.createClient(redis_url.port, redis_url.hostname);
  19. if (redis_url.auth) {
  20. redis.auth(redis_url.auth.split(":")[1]);
  21. }
  22. redis.on("ready", function() {
  23. logging.log("Redis connection established.");
  24. if (process.env.HEROKU) {
  25. logging.log("Running on heroku, flushing redis");
  26. redis.flushall();
  27. }
  28. });
  29. redis.on("error", function (err) {
  30. logging.error(err);
  31. });
  32. redis.on("end", function () {
  33. logging.warn("Redis connection lost!");
  34. });
  35. }
  36. // sets the date of the face file belonging to +skin_hash+ to now
  37. // the helms file is ignored because we only need 1 file to read/write from
  38. function update_file_date(rid, skin_hash) {
  39. if (skin_hash) {
  40. var face_path = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
  41. fs.exists(face_path, function(exists) {
  42. if (exists) {
  43. var date = new Date();
  44. fs.utimes(face_path, date, date, function(err) {
  45. if (err) {
  46. logging.error(rid, "Error:", err.stack);
  47. }
  48. });
  49. } else {
  50. logging.error(rid, "tried to update", face_path + " date, but it does not exist");
  51. }
  52. });
  53. }
  54. }
  55. var exp = {};
  56. // returns the redis instance
  57. exp.get_redis = function() {
  58. return redis;
  59. };
  60. // updates the redis instance's server_info object
  61. // callback: error, info object
  62. exp.info = function(callback) {
  63. redis.info(function (err, res) {
  64. // parse the info command and store it in redis.server_info
  65. // this code block was taken from mranney/node_redis#on_info_cmd
  66. // http://git.io/LBUNbg
  67. var lines = res.toString().split("\r\n");
  68. var obj = {};
  69. lines.forEach(function (line) {
  70. var parts = line.split(":");
  71. if (parts[1]) {
  72. obj[parts[0]] = parts[1];
  73. }
  74. });
  75. obj.versions = [];
  76. if (obj.redis_version) {
  77. obj.redis_version.split(".").forEach(function(num) {
  78. obj.versions.push(+num);
  79. });
  80. }
  81. redis.server_info = obj;
  82. callback(err, redis.server_info);
  83. });
  84. };
  85. // sets the timestamp for +userId+ and its face file's (+hash+) date to the current time
  86. // if +temp+ is true, the timestamp is set so that the record will be outdated after 60 seconds
  87. // these 60 seconds match the duration of Mojang's rate limit ban
  88. // callback: error
  89. exp.update_timestamp = function(rid, userId, hash, temp, callback) {
  90. logging.log(rid, "cache: updating timestamp");
  91. var sub = temp ? (config.local_cache_time - 60) : 0;
  92. var time = Date.now() - sub;
  93. // store userId in lower case if not null
  94. userId = userId && userId.toLowerCase();
  95. redis.hmset(userId, "t", time, function(err) {
  96. callback(err);
  97. });
  98. update_file_date(rid, hash);
  99. };
  100. // create the key +userId+, store +skin_hash+, +cape_hash+ and time
  101. // if either +skin_hash+ or +cape_hash+ are undefined, they will not be stored
  102. // this feature can be used to write both cape and skin at separate times
  103. // +callback+ contans error
  104. exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) {
  105. logging.log(rid, "cache: saving skin:" + skin_hash + " cape:" + cape_hash);
  106. var time = Date.now();
  107. // store shorter null byte instead of "null"
  108. skin_hash = (skin_hash === null ? "" : skin_hash);
  109. cape_hash = (cape_hash === null ? "" : cape_hash);
  110. // store userId in lower case if not null
  111. userId = userId && userId.toLowerCase();
  112. if (skin_hash === undefined) {
  113. redis.hmset(userId, "c", cape_hash, "t", time, function(err) {
  114. callback(err);
  115. });
  116. } else if (cape_hash === undefined) {
  117. redis.hmset(userId, "s", skin_hash, "t", time, function(err) {
  118. callback(err);
  119. });
  120. } else {
  121. redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err) {
  122. callback(err);
  123. });
  124. }
  125. };
  126. // removes the hash for +userId+ from the cache
  127. exp.remove_hash = function(rid, userId) {
  128. logging.log(rid, "cache: deleting hash");
  129. redis.del(userId.toLowerCase(), "h", "t");
  130. };
  131. // get a details object for +userId+
  132. // {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512}
  133. // callback: error, details
  134. // details is null when userId not cached
  135. exp.get_details = function(userId, callback) {
  136. // get userId in lower case if not null
  137. userId = userId && userId.toLowerCase();
  138. redis.hgetall(userId, function(err, data) {
  139. var details = null;
  140. if (data) {
  141. details = {
  142. skin: data.s === "" ? null : data.s,
  143. cape: data.c === "" ? null : data.c,
  144. time: Number(data.t)
  145. };
  146. }
  147. callback(err, details);
  148. });
  149. };
  150. connect_redis();
  151. module.exports = exp;