cache.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var logging = require("./logging");
  2. var config = require("./config");
  3. var redis = null;
  4. var fs = require("fs");
  5. // sets up redis connection
  6. // flushes redis when running on heroku (files aren't kept between pushes)
  7. function connect_redis() {
  8. logging.log("connecting to redis...");
  9. if (process.env.REDISCLOUD_URL) {
  10. var redisURL = require("url").parse(process.env.REDISCLOUD_URL);
  11. redis = require("redis").createClient(redisURL.port, redisURL.hostname);
  12. redis.auth(redisURL.auth.split(":")[1]);
  13. } else {
  14. redis = require("redis").createClient();
  15. }
  16. redis.on("ready", function() {
  17. logging.log("Redis connection established.");
  18. if(process.env.HEROKU) {
  19. logging.log("Running on heroku, flushing redis");
  20. redis.flushall();
  21. }
  22. });
  23. redis.on("error", function (err) {
  24. logging.error(err);
  25. });
  26. redis.on("end", function () {
  27. logging.warn("Redis connection lost!");
  28. });
  29. }
  30. // sets the date of the face file belonging to +hash+ to now
  31. // the helms file is ignored because we only need 1 file to read/write from
  32. function update_file_date(hash) {
  33. if (hash) {
  34. var path = config.faces_dir + hash + ".png";
  35. fs.exists(path, function(exists) {
  36. if (exists) {
  37. var date = new Date();
  38. fs.utimes(path, date, date, function(err){
  39. if (err) {
  40. logging.error(err);
  41. }
  42. });
  43. } else {
  44. logging.error("Tried to update " + path + " date, but it does not exist");
  45. }
  46. });
  47. }
  48. }
  49. var exp = {};
  50. // returns the redis instance
  51. exp.get_redis = function() {
  52. return redis;
  53. };
  54. // updates the redis instance's server_info object
  55. // callback contains error, info object
  56. exp.info = function(callback) {
  57. redis.info(function (err, res) {
  58. // parse the info command and store it in redis.server_info
  59. // this code block was taken from mranney/node_redis#on_info_cmd
  60. // http://git.io/LBUNbg
  61. var lines = res.toString().split("\r\n");
  62. var obj = {};
  63. lines.forEach(function (line) {
  64. var parts = line.split(':');
  65. if (parts[1]) {
  66. obj[parts[0]] = parts[1];
  67. }
  68. });
  69. obj.versions = [];
  70. if( obj.redis_version ){
  71. obj.redis_version.split('.').forEach(function (num) {
  72. obj.versions.push(+num);
  73. });
  74. }
  75. redis.server_info = obj;
  76. callback(err, redis.server_info);
  77. });
  78. };
  79. // sets the timestamp for +uuid+ and its face file's date to now
  80. exp.update_timestamp = function(uuid, hash) {
  81. logging.log(uuid + " cache: updating timestamp");
  82. var time = new Date().getTime();
  83. // store uuid in lower case if not null
  84. uuid = uuid && uuid.toLowerCase();
  85. redis.hmset(uuid, "t", time);
  86. update_file_date(hash);
  87. };
  88. // create the key +uuid+, store +hash+ and time
  89. exp.save_hash = function(uuid, hash) {
  90. logging.log(uuid + " cache: saving hash");
  91. var time = new Date().getTime();
  92. // store shorter null byte instead of "null"
  93. hash = hash || ".";
  94. // store uuid in lower case if not null
  95. uuid = uuid && uuid.toLowerCase();
  96. redis.hmset(uuid, "h", hash, "t", time);
  97. };
  98. // get a details object for +uuid+
  99. // {hash: "0123456789abcdef", time: 1414881524512}
  100. // null when uuid unkown
  101. exp.get_details = function(uuid, callback) {
  102. // get uuid in lower case if not null
  103. uuid = uuid && uuid.toLowerCase();
  104. redis.hgetall(uuid, function(err, data) {
  105. var details = null;
  106. if (data) {
  107. details = {
  108. hash: (data.h == "." ? null : data.h),
  109. time: Number(data.t)
  110. };
  111. }
  112. callback(err, details);
  113. });
  114. };
  115. connect_redis();
  116. module.exports = exp;