UNPKG

2.99 kBTypeScriptView Raw
1/**
2 * This file is part of the @egodigital/egoose distribution.
3 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
4 *
5 * @egodigital/egoose is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation, version 3.
8 *
9 * @egodigital/egoose is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17/**
18 * Describes a cache (client).
19 */
20export interface Cache {
21 /**
22 * Tries to return a value from the cache.
23 *
24 * @param {any} key The key.
25 * @param {TDefault} [defaultValue] The custom default value.
26 *
27 * @return {Promise<TValue|TDefault>} The promise with the value or the default value.
28 */
29 get<TValue = any, TDefault = TValue>(key: any, defaultValue?: TValue): Promise<TValue | TDefault>;
30 /**
31 * Sets a value.
32 *
33 * @param {any} key The key.
34 * @param {any} value The value.
35 * @param {SetCacheValueOptions|null|undefined} [opts] Additional and custom options.
36 *
37 * @return {Promise<boolean>} The promise that indicates if operation was successful or not.
38 */
39 set(key: any, value: any, opts?: SetCacheValueOptions | null | undefined): Promise<boolean>;
40}
41/**
42 * Options for 'Cache.set()' method.
43 */
44export declare type SetCacheValueOptions = {
45 [key: string]: any;
46};
47/**
48 * A basic cache (client).
49 */
50export declare abstract class CacheBase implements Cache {
51 /** @inheritdoc */
52 get<TValue = any, TDefault = TValue>(key: any, defaultValue?: TValue): Promise<TValue | TDefault>;
53 /** @inheritdoc */
54 protected abstract getInner(key: string, defaultValue: any): Promise<any>;
55 /**
56 * Tries to return a value from a key/value pair.
57 *
58 * @param {Object|undefined|null} opts The key/value pair.
59 * @param {any} key The key.
60 * @param {TDefault} [defaultValue] The custom default value.
61 *
62 * @return {TValue|TDefault} The value or the default value if not found.
63 */
64 protected getOptionValue<TValue = any, TDefault = TValue>(opts: {
65 [key: string]: any;
66 } | undefined | null, key: any, defaultValue?: TDefault): TValue | TDefault;
67 /** @inheritdoc */
68 set(key: any, value: any, opts?: SetCacheValueOptions | null | undefined): Promise<boolean>;
69 /**
70 * The logic for 'set()' method.
71 *
72 * @param {string} key The key.
73 * @param {string} defaultValue The default value.
74 * @param {SetCacheValueOptions} opts Custom options.
75 */
76 protected abstract setInner(key: string, defaultValue: any, opts: SetCacheValueOptions): Promise<void>;
77}