cache.js 3.0 KB

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