cache.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 date to now
  85. // +callback+ contains error
  86. exp.update_timestamp = function(rid, userId, hash, callback) {
  87. logging.log(rid + "cache: updating timestamp");
  88. var time = new Date().getTime();
  89. // store userId in lower case if not null
  90. userId = userId && userId.toLowerCase();
  91. redis.hmset(userId, "t", time, function(err) {
  92. callback(err);
  93. });
  94. update_file_date(rid, hash);
  95. };
  96. // create the key +userId+, store +skin_hash+ hash, +cape_hash+ hash and time
  97. // +callback+ contans error
  98. exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) {
  99. logging.log(rid + "cache: saving hash");
  100. logging.log(rid + "skin:" + skin_hash + " cape:" + cape_hash);
  101. var time = new Date().getTime();
  102. // store shorter null byte instead of "null"
  103. skin_hash = skin_hash || ".";
  104. cape_hash = cape_hash || ".";
  105. // store userId in lower case if not null
  106. userId = userId && userId.toLowerCase();
  107. redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err){
  108. callback(err);
  109. });
  110. };
  111. // removes the hash for +userId+ from the cache
  112. exp.remove_hash = function(rid, userId) {
  113. logging.log(rid + "cache: deleting hash");
  114. redis.del(userId.toLowerCase(), "h", "t");
  115. };
  116. // get a details object for +userId+
  117. // {skin: "0123456789abcdef", cape: "gs1gds1g5d1g5ds1", time: 1414881524512}
  118. // null when userId unkown
  119. exp.get_details = function(userId, callback) {
  120. // get userId in lower case if not null
  121. userId = userId && userId.toLowerCase();
  122. redis.hgetall(userId, function(err, data) {
  123. var details = null;
  124. if (data) {
  125. details = {
  126. skin: (data.s === "." ? null : data.s),
  127. cape: (data.c === "." ? null : data.c),
  128. time: Number(data.t)
  129. };
  130. }
  131. callback(err, details);
  132. });
  133. };
  134. connect_redis();
  135. module.exports = exp;