cache.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 +hash+ to now
  36. // the helms file is ignored because we only need 1 file to read/write from
  37. function update_file_date(hash, uuid) {
  38. if (hash) {
  39. var path = config.faces_dir + 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(uuid + " Error: " + err);
  46. }
  47. });
  48. } else {
  49. logging.error(uuid + " 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 +uuid+ and its face file's date to now
  85. exp.update_timestamp = function(uuid, hash) {
  86. logging.log(uuid + " cache: updating timestamp");
  87. var time = new Date().getTime();
  88. // store uuid in lower case if not null
  89. uuid = uuid && uuid.toLowerCase();
  90. redis.hmset(uuid, "t", time);
  91. update_file_date(hash, uuid);
  92. };
  93. // create the key +uuid+, store +hash+ and time
  94. exp.save_hash = function(uuid, hash) {
  95. logging.log(uuid + " cache: saving hash");
  96. var time = new Date().getTime();
  97. // store shorter null byte instead of "null"
  98. hash = hash || ".";
  99. // store uuid in lower case if not null
  100. uuid = uuid && uuid.toLowerCase();
  101. redis.hmset(uuid, "h", hash, "t", time);
  102. };
  103. exp.remove_hash = function(uuid) {
  104. logging.log(uuid + " cache: deleting hash");
  105. redis.del(uuid.toLowerCase(), "h", "t");
  106. };
  107. // get a details object for +uuid+
  108. // {hash: "0123456789abcdef", time: 1414881524512}
  109. // null when uuid unkown
  110. exp.get_details = function(uuid, callback) {
  111. // get uuid in lower case if not null
  112. uuid = uuid && uuid.toLowerCase();
  113. redis.hgetall(uuid, function(err, data) {
  114. var details = null;
  115. if (data) {
  116. details = {
  117. hash: (data.h == "." ? null : data.h),
  118. time: Number(data.t)
  119. };
  120. }
  121. callback(err, details);
  122. });
  123. };
  124. connect_redis();
  125. module.exports = exp;