@egodigital/egoose
Version:
Helper classes and functions for Node.js 10 or later.
150 lines • 4.94 kB
JavaScript
;
/**
* This file is part of the @egodigital/egoose distribution.
* Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
*
* @egodigital/egoose is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* @egodigital/egoose is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const redis = require("redis");
const index_1 = require("../index");
const index_2 = require("./index");
/**
* A Redis cache (client).
*/
class RedisCache extends index_2.CacheBase {
/**
* Initializes a new instance of that class.
*
* @param {RedisCacheOptions} options The options.
*/
constructor(options) {
super();
this.options = options;
}
createClient() {
let host = index_1.toStringSafe(this.options.host)
.trim();
if ('' === host) {
host = '127.0.0.1';
}
let port = parseInt(index_1.toStringSafe(this.options.port)
.trim());
if (isNaN(port)) {
port = 6379;
}
return redis.createClient({
host: host,
port: port,
});
}
/**
* Creates a new instance from environment variable.
*
* @return {RedisCache} The new instance.
*/
static fromEnvironment() {
return new RedisCache({
host: process.env.REDIS_HOST,
port: parseInt(index_1.toStringSafe(process.env.REDIS_HOST_PORT)
.trim())
});
}
/** @inheritdoc */
async getInner(key, defaultValue) {
return await this.withConnection((client) => {
return new Promise((resolve, reject) => {
try {
client.get(key, (err, value) => {
try {
if (err) {
reject(err);
}
else {
if (_.isNil(value)) {
resolve(defaultValue);
}
else {
resolve(JSON.parse(value));
}
}
}
catch (e) {
reject(e);
}
});
}
catch (e) {
reject(e);
}
});
});
}
/** @inheritdoc */
async setInner(key, value, opts) {
let ttl = parseInt(index_1.toStringSafe(this.getOptionValue(opts, 'ttl'))
.trim());
if (isNaN(ttl)) {
ttl = false;
}
await this.withConnection((client) => {
return new Promise((resolve, reject) => {
try {
if (_.isNil(value)) {
// no data => delete
client.del(key, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
}
else {
const VALUE_TO_SAVE = JSON.stringify(value);
const CALLBACK = (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
};
if (false === ttl) {
client.set(key, VALUE_TO_SAVE, CALLBACK);
}
else {
client.set(key, VALUE_TO_SAVE, 'EX', ttl, CALLBACK);
}
}
}
catch (e) {
reject(e);
}
});
});
}
async withConnection(func) {
const CLIENT = this.createClient();
try {
return await Promise.resolve(func(CLIENT));
}
finally {
CLIENT.quit();
}
}
}
exports.RedisCache = RedisCache;
//# sourceMappingURL=redis.js.map