@egodigital/egoose
Version:
Helper classes and functions for Node.js 10 or later.
72 lines • 2.28 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 index_1 = require("../index");
/**
* A basic cache (client).
*/
class CacheBase {
/** @inheritdoc */
async get(key, defaultValue) {
try {
return await this.getInner(normalizeCacheKey(key), defaultValue);
}
catch {
return defaultValue;
}
}
/**
* Tries to return a value from a key/value pair.
*
* @param {Object|undefined|null} opts The key/value pair.
* @param {any} key The key.
* @param {TDefault} [defaultValue] The custom default value.
*
* @return {TValue|TDefault} The value or the default value if not found.
*/
getOptionValue(opts, key, defaultValue) {
key = index_1.normalizeString(key);
if (opts) {
for (const PROP in opts) {
if (index_1.normalizeString(PROP) === key) {
return opts[PROP];
}
}
}
return defaultValue;
}
/** @inheritdoc */
async set(key, value, opts) {
if (_.isNil(opts)) {
opts = {};
}
try {
await this.setInner(normalizeCacheKey(key), value, opts);
return true;
}
catch {
return false;
}
}
}
exports.CacheBase = CacheBase;
function normalizeCacheKey(key) {
return index_1.normalizeString(key);
}
//# sourceMappingURL=index.js.map