cache.js 5.1 KB

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