-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.interface.ts
155 lines (140 loc) · 4.7 KB
/
cache.interface.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { CacheItemInterface } from './cache-item.interface';
import type { CacheException } from './exceptions';
export type GetOptions<T> = {
defaultValue?: T;
disableCache?: boolean;
};
export type DeleteOptions = {
/** Whether to disable caching, by default false */
disableCache?: boolean;
};
export type HasOptions = {
/** Whether to disable caching, by default false */
disableCache?: boolean;
/** Callback since Cache.has can't really return CacheException... */
onError?: (error: CacheException) => void;
};
export type SetOptions = {
/** Time-To-Live expressed in seconds, 0 meaning forever */
ttl?: number;
/** Whether to disable caching, by default false */
disableCache?: boolean;
};
export type GetOrSetOptions = Omit<SetOptions, 'disableCache'> & {
/**
* Whether to disable caching, by default false.
* Accepts an object to selectively disable writes and/or reads
*/
disableCache?:
| boolean
| {
/** True to disable cache reads */
read: boolean;
/** True to disable cache writes */
write: boolean;
};
onError?: (errors: CacheException[]) => void;
};
type CacheValueProviderParams = { key: CacheKey };
export type CacheProviderAsyncFn<T> = (
params?: CacheValueProviderParams
) => Promise<T>;
export type CacheProviderSyncFn<T> = (params?: CacheValueProviderParams) => T;
export type CacheValueProviderFn<T> =
| CacheProviderAsyncFn<T>
| CacheProviderSyncFn<T>;
export type CacheKey = string;
export interface CacheInterface<TBase = string, KBase = CacheKey> {
/**
* Fetches a value from the cache
*
* @param key - The unique key of this item in the cache.
* @param options - An object holding GetOptions
*
* @returns A promise returning a CacheItemInterface, or defaultValue in case of cache miss.
*/
get<T = TBase, K extends KBase = KBase>(
key: K,
options?: GetOptions<T>
): Promise<CacheItemInterface<T>>;
/**
* Persists data in the cache, uniquely referenced by a key.
*
* @param key - The key of the item to store.
* @param value - The value of the item to store or a function returning the value. Must be serializable.
* @param options - An object holding SetOptions
*/
set<T = TBase, K extends KBase = KBase>(
key: K,
value: T | CacheValueProviderFn<T>,
options?: SetOptions
): Promise<boolean | CacheException>;
/**
* Delete an item from the cache by its unique key.
*
* @param key - The unique cache key of the item to delete.
*
* @return True if the item was successfully removed, false if it did not exists.
* CacheException if there was an error.
*/
delete<K extends KBase = KBase>(
key: K,
options?: DeleteOptions
): Promise<boolean | CacheException>;
/**
* Determines whether an item is present in the cache.
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it, making the state of your app out of date.
*
* @param key - The cache item key
*
* @return True if the item exists in the cache and was removed, false otherwise.
* Undefined is used to determine if the operation was successful.
* If cacheDisabled option is set to true, it will always return false.
*/
has<K extends KBase = KBase>(
key: K,
options?: HasOptions
): Promise<boolean | undefined>;
/**
* Obtains multiple cache items by their unique keys.
*
* @param keys - A list of keys that can obtained in a single operation.
*/
getMultiple<T = TBase, K extends KBase = KBase>(
keys: K[],
options?: GetOptions<T>
): Promise<Map<K, CacheItemInterface<T>>>;
/**
* Persists a set of key => value pairs in the cache.
*
* @param keyVals - array of tuples container key and value
*/
setMultiple<T = TBase, K extends KBase = KBase>(
keyVals: Readonly<[K, T | CacheValueProviderFn<T>][]>,
options?: SetOptions
): Promise<Map<K, boolean | CacheException>>;
/**
* Delete cache entries from multiple keys
*
* @param keys - A list of keys that should be deleted.
*/
deleteMultiple<K extends KBase = KBase>(
keys: K[],
options?: DeleteOptions
): Promise<Map<K, boolean | CacheException>>;
/**
* Delete the entire cache's keys.
*
* @return bool True on success and CacheException on failure.
*/
clear(): Promise<true | CacheException>;
getOrSet<T = TBase, K extends KBase = KBase>(
key: K,
value: T | CacheValueProviderFn<T>,
options?: GetOrSetOptions
): Promise<CacheItemInterface<T>>;
}