Files
node-pkg-express-utils/out/redis.js
Sebastian Seedorf 7ad9cd1904 inital #4
2020-11-16 12:44:08 +01:00

77 lines
2.7 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Redis = void 0;
const redis = require("redis");
const _1 = require(".");
const util_1 = require("util");
class RealRedis {
get client() {
if (this._client !== undefined)
return this._client;
_1.DefaultConfig.requireEnv('REDIS_URL');
this._client = redis.createClient({ url: _1.DefaultConfig.REDIS_URL });
return this._client;
}
get(key) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._promGet)
this._promGet = util_1.promisify(this.client.get);
const value = yield this._promGet(key);
// eslint-disable-next-line no-null/no-null
return value === null ? undefined : value;
});
}
set(key, value) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._promSet)
this._promSet = util_1.promisify(this.client.set);
yield this._promSet(key, value);
});
}
del(...keys) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._promDel)
this._promDel = util_1.promisify(this.client.del);
return yield this._promDel(...keys);
});
}
}
class MockRedis {
constructor() {
this.inMemory = {};
}
get client() {
return undefined;
}
get(key) {
return __awaiter(this, void 0, void 0, function* () {
return this.inMemory[key] || undefined;
});
}
set(key, value) {
return __awaiter(this, void 0, void 0, function* () {
this.inMemory[key] = value;
});
}
del(...keys) {
return __awaiter(this, void 0, void 0, function* () {
for (const key of keys) {
delete this.inMemory[key];
}
return keys.length;
});
}
}
exports.Redis = _1.DefaultConfig.REDIS_URL || _1.DefaultConfig.isProduction
? new RealRedis()
: new MockRedis();