cache.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var config = require("./config");
  2. var redis = null;
  3. var fs = require("fs");
  4. // sets up redis connection
  5. function connect_redis() {
  6. console.log("connecting to redis...");
  7. if (process.env.REDISCLOUD_URL) {
  8. var redisURL = require("url").parse(process.env.REDISCLOUD_URL);
  9. redis = require("redis").createClient(redisURL.port, redisURL.hostname);
  10. redis.auth(redisURL.auth.split(":")[1]);
  11. } else {
  12. redis = require("redis").createClient();
  13. }
  14. redis.on("ready", function() {
  15. console.log("Redis connection established.");
  16. });
  17. redis.on("error", function (err) {
  18. console.error(err);
  19. });
  20. redis.on("end", function () {
  21. console.warn("Redis connection lost!");
  22. });
  23. }
  24. // flushes redis, deletes faces + helms
  25. // function clear_cache() {
  26. // console.log("Flushing redis");
  27. // redis.flushall();
  28. // console.log("Deleting all faces + helms...");
  29. // fs.readdir(config.faces_dir, function(err, files) {
  30. // if (err) {
  31. // console.error(err);
  32. // } else {
  33. // for (var i in files) {
  34. // var file = files[i];
  35. // if (file[0] != ".") {
  36. // // delete face file
  37. // fs.unlink(config.faces_dir + file, function(err){
  38. // if (err) {
  39. // console.error(err);
  40. // }
  41. // });
  42. // // delete helm file, we assume this exists as well
  43. // fs.unlink(config.helms_dir + file, function(err){
  44. // if (err) {
  45. // console.error(err);
  46. // }
  47. // });
  48. // }
  49. // }
  50. // }
  51. // });
  52. // }
  53. // sets the date of the face file belonging to +hash+ to now
  54. // the helms file is ignored because we only need 1 file to read/write from
  55. function update_file_date(hash) {
  56. if (hash) {
  57. var path = config.faces_dir + hash + ".png";
  58. fs.exists(path, function(exists) {
  59. if (exists) {
  60. var date = new Date();
  61. fs.utimes(path, date, date, function(err){
  62. if (err) {
  63. console.error(err);
  64. }
  65. });
  66. } else {
  67. console.error("Tried to update " + path + " date, but it doesn't exist");
  68. }
  69. });
  70. }
  71. }
  72. var exp = {};
  73. exp.get_redis = function() {
  74. return redis;
  75. };
  76. // sets the timestamp for +uuid+ and its face file's date to now
  77. exp.update_timestamp = function(uuid, hash) {
  78. console.log(uuid + " cache: updating timestamp");
  79. var time = new Date().getTime();
  80. redis.hmset(uuid, "t", time);
  81. update_file_date(hash);
  82. };
  83. // create the key +uuid+, store +hash+ and time
  84. exp.save_hash = function(uuid, hash) {
  85. console.log(uuid + " cache: saving hash");
  86. var time = new Date().getTime();
  87. redis.hmset(uuid, "h", hash, "t", time);
  88. };
  89. // get a details object for +uuid+
  90. // {hash: "0123456789abcdef", time: 1414881524512}
  91. // null when uuid unkown
  92. exp.get_details = function(uuid, callback) {
  93. redis.hgetall(uuid, function(err, data) {
  94. var details = null;
  95. if (data) {
  96. details = {
  97. hash: (data.h == "null" ? null : data.h),
  98. time: Number(data.t)
  99. };
  100. }
  101. callback(err, details);
  102. });
  103. };
  104. connect_redis();
  105. module.exports = exp;