1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2025-09-04 04:50:14 +02:00

Rewrote redis storage handler

also removed unnecessary newline in file storage handler
This commit is contained in:
zneix 2020-09-01 23:43:54 +02:00
parent a4cf027900
commit c165781b18
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 911916E0523B22F6
6 changed files with 67 additions and 151 deletions

View file

@ -2,7 +2,6 @@ const winston = require('winston');
const fs = require('fs');
const crypto = require('crypto');
class FileDocumentStore {
constructor(options){

View file

@ -1,89 +1,49 @@
const redis = require('redis');
const Redis = require('ioredis');
const winston = require('winston');
// For storing in redis
// options[type] = redis
// options[host] - The host to connect to (default localhost)
// options[port] - The port to connect to (default 5379)
// options[db] - The db to use (default 0)
// options[expire] - The time to live for each key set (default never)
class RedisDocumentStore {
var RedisDocumentStore = function(options, client){
this.expire = options.expire;
if (client){
winston.info('using predefined redis client');
RedisDocumentStore.client = client;
} else if (!RedisDocumentStore.client){
winston.info('configuring redis');
RedisDocumentStore.connect(options);
}
};
// Create a connection according to config
RedisDocumentStore.connect = function(options){
var host = options.host || '127.0.0.1';
var port = options.port || 6379;
var index = options.db || 0;
RedisDocumentStore.client = redis.createClient(port, host);
// authenticate if password is provided
if (options.password){
RedisDocumentStore.client.auth(options.password);
}
RedisDocumentStore.client.on('error', function(err){
winston.error('redis disconnected', err);
});
RedisDocumentStore.client.select(index, function(err){
if (err){
winston.error(
'error connecting to redis index ' + index,
{ error: err }
);
constructor(options = {}){
this.expire = options.expire;
const redisClient = new Redis(options.redisOptions);
redisClient.on('error', err => {
winston.error('redisClient errored', {error: err});
process.exit(1);
}
else {
winston.info('connected to redis on ' + host + ':' + port + '/' + index);
}
});
};
});
redisClient.on('ready', () => {
winston.info(`connected to redis on ${redisClient.options.host}:${redisClient.options.port}/${redisClient.options.db}`);
});
this.client = redisClient;
winston.info('initialized redis client');
}
// Save file in a key
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire){
var _this = this;
RedisDocumentStore.client.set(key, data, function(err){
if (err){
async set(key, data, callback, skipExpire){
await this.client.set(key, data).catch(err => {
winston.error('failed to call redisClient.set', {error: err});
callback(false);
}
else {
if (!skipExpire){
_this.setExpiration(key);
}
callback(true);
}
});
};
return;
});
if (!skipExpire) this.setExpiration(key);
callback(true);
}
// Expire a key in expire time if set
RedisDocumentStore.prototype.setExpiration = function(key){
if (this.expire){
RedisDocumentStore.client.expire(key, this.expire, function(err){
if (err){
winston.error('failed to set expiry on key: ' + key);
}
async get(key, callback, skipExpire){
let data = await this.client.get(key).catch(err => {
winston.error('failed to get document from redis', {key: key, error: err});
callback(false);
return;
});
if (!skipExpire) this.setExpiration(key);
callback(data);
}
async setExpiration(key){
if (!this.expire) return;
await this.client.expire(key, this.expire).catch(err => {
winston.warn('failed to set expiry on key', {key: key, error: err});
});
}
};
}
// Get a file from a key
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);
});
};
module.exports = RedisDocumentStore;
module.exports = RedisDocumentStore;