diff --git a/TODO.md b/TODO.md index 1dfa615..31d642c 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,5 @@ # TODO for OSS * tests * fix that chrome bug where it loads the doc twice -* kick expiration back by increment on each view * Add file extensions ourselves to push state * add feedback for errors to UI - esp. too long diff --git a/lib/document_handler.js b/lib/document_handler.js index 2ef7421..0a2ef27 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -11,7 +11,7 @@ var DocumentHandler = function(options) { }; // Handle retrieving a document -DocumentHandler.prototype.handleGet = function(key, response) { +DocumentHandler.prototype.handleGet = function(key, response, skipExpire) { this.store.get(key, function(ret) { if (ret) { winston.verbose('retrieved document', { key: key }); @@ -23,7 +23,7 @@ DocumentHandler.prototype.handleGet = function(key, response) { response.writeHead(404, { 'content-type': 'application/json' }); response.end(JSON.stringify({ message: 'document not found' })); } - }); + }, skipExpire); }; // Handle adding a new Document diff --git a/lib/file_document_store.js b/lib/file_document_store.js index 03dd81e..da24e67 100644 --- a/lib/file_document_store.js +++ b/lib/file_document_store.js @@ -12,7 +12,7 @@ var FileDocumentStore = function(options) { }; // Save data in a file, key as md5 - since we don't know what we could be passed here -FileDocumentStore.prototype.set = function(key, data, callback) { +FileDocumentStore.prototype.set = function(key, data, callback, setExpire) { try { var _this = this; fs.mkdir(this.basePath, '700', function() { @@ -31,7 +31,7 @@ FileDocumentStore.prototype.set = function(key, data, callback) { }; // Get data from a file from key -FileDocumentStore.prototype.get = function(key, callback) { +FileDocumentStore.prototype.get = function(key, callback, setExpire) { fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) { if (err) { callback(false); diff --git a/lib/redis_document_store.js b/lib/redis_document_store.js index 163b397..17dcc58 100644 --- a/lib/redis_document_store.js +++ b/lib/redis_document_store.js @@ -53,7 +53,7 @@ RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) { RedisDocumentStore.prototype.setExpiration = function(key) { if (this.expire) { RedisDocumentStore.client.expire(key, this.expire, function(err, reply) { - if (err || !reply) { + if (err) { winston.error('failed to set expiry on key: ' + key); } }); @@ -61,8 +61,12 @@ RedisDocumentStore.prototype.setExpiration = function(key) { }; // Get a file from a key -RedisDocumentStore.prototype.get = function(key, callback) { +RedisDocumentStore.prototype.get = function(key, callback, skipExpire) { + var _this = this; RedisDocumentStore.client.get(key, function(err, reply) { + if (!err && !skipExpire) { + _this.setExpiration(key); + } callback(err ? false : reply); }); }; diff --git a/server.js b/server.js index 5f19095..330c9f3 100644 --- a/server.js +++ b/server.js @@ -89,7 +89,8 @@ connect.createServer( }); // get documents app.get('/documents/:id', function(request, response, next) { - return documentHandler.handleGet(request.params.id, response); + var skipExpire = !!config.documents[request.params.id]; + return documentHandler.handleGet(request.params.id, response, skipExpire); }); }), // Otherwise, static