cache.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. redis.hmset(uuid, "t", time);
  58. update_file_date(hash);
  59. };
  60. // create the key +uuid+, store +hash+ and time
  61. exp.save_hash = function(uuid, hash) {
  62. logging.log(uuid + " cache: saving hash");
  63. var time = new Date().getTime();
  64. // store shorter null byte instead of "null"
  65. hash = hash || ".";
  66. // store uuid in lower case if not null
  67. uuid = uuid && uuid.toLowerCase();
  68. redis.hmset(uuid, "h", hash, "t", time);
  69. };
  70. // get a details object for +uuid+
  71. // {hash: "0123456789abcdef", time: 1414881524512}
  72. // null when uuid unkown
  73. exp.get_details = function(uuid, callback) {
  74. redis.hgetall(uuid, function(err, data) {
  75. var details = null;
  76. if (data) {
  77. details = {
  78. hash: (data.h == "." ? null : data.h),
  79. time: Number(data.t)
  80. };
  81. }
  82. callback(err, details);
  83. });
  84. };
  85. connect_redis();
  86. module.exports = exp;