cache.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. exp.get_redis = function() {
  51. return redis;
  52. };
  53. // sets the timestamp for +uuid+ and its face file's date to now
  54. exp.update_timestamp = function(uuid, hash) {
  55. logging.log(uuid + " cache: updating timestamp");
  56. var time = new Date().getTime();
  57. // store uuid in lower case if not null
  58. uuid = uuid && uuid.toLowerCase();
  59. redis.hmset(uuid, "t", time);
  60. update_file_date(hash);
  61. };
  62. // create the key +uuid+, store +hash+ and time
  63. exp.save_hash = function(uuid, hash) {
  64. logging.log(uuid + " cache: saving hash");
  65. var time = new Date().getTime();
  66. // store shorter null byte instead of "null"
  67. hash = hash || ".";
  68. // store uuid in lower case if not null
  69. uuid = uuid && uuid.toLowerCase();
  70. redis.hmset(uuid, "h", hash, "t", time);
  71. };
  72. // get a details object for +uuid+
  73. // {hash: "0123456789abcdef", time: 1414881524512}
  74. // null when uuid unkown
  75. exp.get_details = function(uuid, callback) {
  76. // get uuid in lower case if not null
  77. uuid = uuid && uuid.toLowerCase();
  78. redis.hgetall(uuid, function(err, data) {
  79. var details = null;
  80. if (data) {
  81. details = {
  82. hash: (data.h == "." ? null : data.h),
  83. time: Number(data.t)
  84. };
  85. }
  86. callback(err, details);
  87. });
  88. };
  89. connect_redis();
  90. module.exports = exp;