Skip to content

Commit d0e3e4c

Browse files
committed
Support for redis expiration
1 parent 0f2075f commit d0e3e4c

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

TODO

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ cache static in memory
66
add feedback for errors to UI - esp. too long
77
copy URL to clipboard button
88
add about page
9-
support built-in expiration
109

1110
# shared version only
1211
some way to do announcements easily (and use for ads)

config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"type": "redis",
2020
"host": "localhost",
2121
"port": 6379,
22-
"db": 2
22+
"db": 2,
23+
"expire": 3600
2324
}
2425

2526
}

lib/file_document_store.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var winston = require('winston');
44
var hashlib = require('hashlib');
55

66
// For storing in files
7+
// options[type] = file
8+
// options[path] - Where to store
79

810
var FileDocumentStore = function(options) {
911
this.basePath = options.path || './data';

lib/redis_document_store.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ var redis = require('redis');
22
var winston = require('winston');
33
var hashlib = require('hashlib');
44

5+
// For storing in redis
6+
// options[type] = redis
7+
// options[host] - The host to connect to (default localhost)
8+
// options[port] - The port to connect to (default 5379)
9+
// options[db] - The db to use (default 0)
10+
// options[expire] - The time to live for each key set (default never)
11+
512
var RedisDocumentStore = function(options) {
13+
this.expire = options.expire;
614
if (!RedisDocumentStore.client) {
715
RedisDocumentStore.connect(options);
816
}
@@ -27,11 +35,31 @@ RedisDocumentStore.connect = function(options) {
2735

2836
// Save file in a key
2937
RedisDocumentStore.prototype.set = function(key, data, callback) {
38+
var _this = this;
3039
RedisDocumentStore.client.set(key, data, function(err, reply) {
31-
callback(!err);
40+
if (err) {
41+
callback(false);
42+
}
43+
else {
44+
_this.setExpiration(key);
45+
callback(true);
46+
}
3247
});
3348
};
3449

50+
// Expire a key in expire time if set
51+
RedisDocumentStore.prototype.setExpiration = function(key) {
52+
if (this.expire) {
53+
RedisDocumentStore.client.expire(key, this.expire, function(err, reply) {
54+
if (err || !reply) {
55+
winston.error('failed to set expiry on key: ' + key);
56+
} else {
57+
console.log('set');
58+
}
59+
});
60+
}
61+
};
62+
3563
// Get a file from a key
3664
RedisDocumentStore.prototype.get = function(key, callback) {
3765
RedisDocumentStore.client.get(key, function(err, reply) {

server.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ if (config.logging) {
2828

2929
// build the store from the config on-demand - so that we don't load it
3030
// for statics
31-
var preferredStore = function() {
32-
if (!config.storage) {
33-
config.storage = { type: 'file' };
34-
}
35-
if (!config.storage.type) {
36-
config.storage.type = 'file';
37-
}
38-
var Store = require('./lib/' + config.storage.type + '_document_store');
39-
return new Store(config.storage);
40-
};
31+
if (!config.storage) {
32+
config.storage = { type: 'file' };
33+
}
34+
if (!config.storage.type) {
35+
config.storage.type = 'file';
36+
}
37+
var Store = require('./lib/' + config.storage.type + '_document_store');
38+
var preferredStore = new Store(config.storage);
4139

4240
// Set the server up and listen forever
4341
http.createServer(function(request, response) {
@@ -48,15 +46,15 @@ http.createServer(function(request, response) {
4846
handler = new DocumentHandler({
4947
keyLength: config.keyLength,
5048
maxLength: config.maxLength,
51-
store: preferredStore()
49+
store: preferredStore
5250
});
5351
return handler.handlePost(request, response);
5452
}
5553
// Looking up a doc
5654
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
5755
if (request.method == 'GET' && match) {
5856
handler = new DocumentHandler({
59-
store: preferredStore()
57+
store: preferredStore
6058
});
6159
return handler.handleGet(match[1], response);
6260
}

0 commit comments

Comments
 (0)