Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
use redis expire event to delete stored data immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuggett committed Aug 26, 2017
1 parent 01a064e commit 07b6f6f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ redis_client.on('error', err => {
log.error('Redis:', err);
});

const redis_sub = redis_client.duplicate();

const subKey = '__keyevent@0__:expired';
redis_sub.psubscribe(subKey, function() {
log.info('Redis:', 'subscribed to expired key events');
});

let tempDir = null;

if (config.s3_bucket) {
Expand All @@ -43,6 +50,11 @@ if (config.s3_bucket) {
quit: quit,
metadata
};

redis_sub.on('pmessage', function(channel, message, id) {
log.info('RedisExpired:', id);
awsForceDelete(id);
});
} else {
tempDir = fs.mkdtempSync(`${tmpdir()}${path.sep}send-`);
log.info('tempDir', tempDir);
Expand All @@ -61,6 +73,11 @@ if (config.s3_bucket) {
quit: quit,
metadata
};

redis_sub.on('pmessage', function(channel, message, id) {
log.info('RedisExpired:', id);
localForceDelete(id);
});
}

function flushall() {
Expand Down Expand Up @@ -175,6 +192,7 @@ function localDelete(id, delete_token) {
function localForceDelete(id) {
return new Promise((resolve, reject) => {
redis_client.del(id);
log.info('Deleted:', id);
resolve(fs.unlinkSync(path.join(tempDir, id)));
});
}
Expand Down Expand Up @@ -270,6 +288,7 @@ function awsForceDelete(id) {
Key: id
};

log.info('Deleted:', id);
s3.deleteObject(params, function(err, _data) {
redis_client.del(id);
err ? reject(err) : resolve();
Expand Down

0 comments on commit 07b6f6f

Please sign in to comment.