update file date on hash update (face only)

This commit is contained in:
jomo 2014-11-04 23:43:39 +01:00
parent 94911167e6
commit 939d8f10a4
2 changed files with 26 additions and 5 deletions

View File

@ -1,9 +1,10 @@
var config = require("./config"); var config = require("./config");
var redis = null; var redis = null;
var fs = require("fs");
function connect_redis() { function connect_redis() {
console.log("connecting to redis"); console.log("connecting to redis...");
if (process.env.REDISCLOUD_URL) { if (process.env.REDISCLOUD_URL) {
var redisURL = require("url").parse(process.env.REDISCLOUD_URL); var redisURL = require("url").parse(process.env.REDISCLOUD_URL);
redis = require("redis").createClient(redisURL.port, redisURL.hostname); redis = require("redis").createClient(redisURL.port, redisURL.hostname);
@ -12,7 +13,7 @@ function connect_redis() {
redis = require("redis").createClient(); redis = require("redis").createClient();
} }
redis.on("ready", function() { redis.on("ready", function() {
console.log("Redis connection established."); console.log("Redis connection established. Flushing all data.");
redis.flushall(); redis.flushall();
}); });
redis.on("error", function (err) { redis.on("error", function (err) {
@ -23,17 +24,37 @@ function connect_redis() {
}); });
} }
// sets the date of the face file belonging to +hash+ to now
function update_file_date(hash) {
if (hash) {
var path = config.faces_dir + hash + ".png";
fs.exists(path, function(exists) {
if (exists) {
var date = new Date();
fs.utimes("path", date, date, function(err){
if (err) {
console.error(err);
}
});
} else {
console.error("Tried to update " + path + " date, but it doesn't exist");
}
});
}
}
var exp = {}; var exp = {};
exp.get_redis = function() { exp.get_redis = function() {
return redis; return redis;
}; };
// sets the timestamp for +uuid+ to now // sets the timestamp for +uuid+ and its face file's date to now
exp.update_timestamp = function(uuid) { exp.update_timestamp = function(uuid, hash) {
console.log(uuid + " cache: updating timestamp"); console.log(uuid + " cache: updating timestamp");
var time = new Date().getTime(); var time = new Date().getTime();
redis.hmset(uuid, "t", time); redis.hmset(uuid, "t", time);
update_file_date(hash);
}; };
// create the key +uuid+, store +hash+ and time // create the key +uuid+, store +hash+ and time

View File

@ -30,7 +30,7 @@ function store_images(uuid, details, callback) {
if (details && details.hash == hash) { if (details && details.hash == hash) {
// hash hasn't changed // hash hasn't changed
console.log(uuid + " hash has not changed"); console.log(uuid + " hash has not changed");
cache.update_timestamp(uuid); cache.update_timestamp(uuid, hash);
callback(null, hash); callback(null, hash);
} else { } else {
// hash has changed // hash has changed