UNPKG

2.29 kBJavaScriptView Raw
1"use strict";
2/**
3 * This file is part of the @egodigital/egoose distribution.
4 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
5 *
6 * @egodigital/egoose is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * @egodigital/egoose is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19const _ = require("lodash");
20const index_1 = require("../index");
21/**
22 * A basic cache (client).
23 */
24class CacheBase {
25 /** @inheritdoc */
26 async get(key, defaultValue) {
27 try {
28 return await this.getInner(normalizeCacheKey(key), defaultValue);
29 }
30 catch (_a) {
31 return defaultValue;
32 }
33 }
34 /**
35 * Tries to return a value from a key/value pair.
36 *
37 * @param {Object|undefined|null} opts The key/value pair.
38 * @param {any} key The key.
39 * @param {TDefault} [defaultValue] The custom default value.
40 *
41 * @return {TValue|TDefault} The value or the default value if not found.
42 */
43 getOptionValue(opts, key, defaultValue) {
44 key = index_1.normalizeString(key);
45 if (opts) {
46 for (const PROP in opts) {
47 if (index_1.normalizeString(PROP) === key) {
48 return opts[PROP];
49 }
50 }
51 }
52 return defaultValue;
53 }
54 /** @inheritdoc */
55 async set(key, value, opts) {
56 if (_.isNil(opts)) {
57 opts = {};
58 }
59 try {
60 await this.setInner(normalizeCacheKey(key), value, opts);
61 return true;
62 }
63 catch (_a) {
64 return false;
65 }
66 }
67}
68exports.CacheBase = CacheBase;
69function normalizeCacheKey(key) {
70 return index_1.normalizeString(key);
71}
72//# sourceMappingURL=index.js.map
\No newline at end of file