@cacheable/memory 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,270 @@
1
+ import { Hookified } from 'hookified';
2
+ import { WrapFunctionOptions } from '@cacheable/memoize';
3
+ import { HashAlgorithm, CacheableStoreItem, CacheableItem } from '@cacheable/utils';
4
+ export { CacheableItem, CacheableStoreItem, HashAlgorithm, hash, hashToNumber } from '@cacheable/utils';
5
+
6
+ type StoreHashAlgorithmFunction = (key: string, storeHashSize: number) => number;
7
+ /**
8
+ * @typedef {Object} CacheableMemoryOptions
9
+ * @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
10
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
11
+ * undefined then it will not have a time-to-live.
12
+ * @property {boolean} [useClone] - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
13
+ * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
14
+ * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
15
+ * @property {number} [storeHashSize] - The number of how many Map stores we have for the hash. Default is 10.
16
+ */
17
+ type CacheableMemoryOptions = {
18
+ ttl?: number | string;
19
+ useClone?: boolean;
20
+ lruSize?: number;
21
+ checkInterval?: number;
22
+ storeHashSize?: number;
23
+ storeHashAlgorithm?: HashAlgorithm | ((key: string, storeHashSize: number) => number);
24
+ };
25
+ type SetOptions = {
26
+ ttl?: number | string;
27
+ expire?: number | Date;
28
+ };
29
+ declare const defaultStoreHashSize = 16;
30
+ declare const maximumMapSize = 16777216;
31
+ declare class CacheableMemory extends Hookified {
32
+ private _lru;
33
+ private _storeHashSize;
34
+ private _storeHashAlgorithm;
35
+ private _store;
36
+ private _ttl;
37
+ private _useClone;
38
+ private _lruSize;
39
+ private _checkInterval;
40
+ private _interval;
41
+ /**
42
+ * @constructor
43
+ * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
44
+ */
45
+ constructor(options?: CacheableMemoryOptions);
46
+ /**
47
+ * Gets the time-to-live
48
+ * @returns {number|string|undefined} - The time-to-live in miliseconds or a human-readable format. If undefined, it will not have a time-to-live.
49
+ */
50
+ get ttl(): number | string | undefined;
51
+ /**
52
+ * Sets the time-to-live
53
+ * @param {number|string|undefined} value - The time-to-live in miliseconds or a human-readable format (example '1s' = 1 second, '1h' = 1 hour). If undefined, it will not have a time-to-live.
54
+ */
55
+ set ttl(value: number | string | undefined);
56
+ /**
57
+ * Gets whether to use clone
58
+ * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
59
+ */
60
+ get useClone(): boolean;
61
+ /**
62
+ * Sets whether to use clone
63
+ * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
64
+ */
65
+ set useClone(value: boolean);
66
+ /**
67
+ * Gets the size of the LRU cache
68
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
69
+ */
70
+ get lruSize(): number;
71
+ /**
72
+ * Sets the size of the LRU cache
73
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
74
+ */
75
+ set lruSize(value: number);
76
+ /**
77
+ * Gets the check interval
78
+ * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
79
+ */
80
+ get checkInterval(): number;
81
+ /**
82
+ * Sets the check interval
83
+ * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
84
+ */
85
+ set checkInterval(value: number);
86
+ /**
87
+ * Gets the size of the cache
88
+ * @returns {number} - The size of the cache
89
+ */
90
+ get size(): number;
91
+ /**
92
+ * Gets the number of hash stores
93
+ * @returns {number} - The number of hash stores
94
+ */
95
+ get storeHashSize(): number;
96
+ /**
97
+ * Sets the number of hash stores. This will recreate the store and all data will be cleared
98
+ * @param {number} value - The number of hash stores
99
+ */
100
+ set storeHashSize(value: number);
101
+ /**
102
+ * Gets the store hash algorithm
103
+ * @returns {HashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
104
+ */
105
+ get storeHashAlgorithm(): HashAlgorithm | StoreHashAlgorithmFunction;
106
+ /**
107
+ * Sets the store hash algorithm. This will recreate the store and all data will be cleared
108
+ * @param {HashAlgorithm | HashAlgorithmFunction} value - The store hash algorithm
109
+ */
110
+ set storeHashAlgorithm(value: HashAlgorithm | StoreHashAlgorithmFunction);
111
+ /**
112
+ * Gets the keys
113
+ * @returns {IterableIterator<string>} - The keys
114
+ */
115
+ get keys(): IterableIterator<string>;
116
+ /**
117
+ * Gets the items
118
+ * @returns {IterableIterator<CacheableStoreItem>} - The items
119
+ */
120
+ get items(): IterableIterator<CacheableStoreItem>;
121
+ /**
122
+ * Gets the store
123
+ * @returns {Array<Map<string, CacheableStoreItem>>} - The store
124
+ */
125
+ get store(): Array<Map<string, CacheableStoreItem>>;
126
+ /**
127
+ * Gets the value of the key
128
+ * @param {string} key - The key to get the value
129
+ * @returns {T | undefined} - The value of the key
130
+ */
131
+ get<T>(key: string): T | undefined;
132
+ /**
133
+ * Gets the values of the keys
134
+ * @param {string[]} keys - The keys to get the values
135
+ * @returns {T[]} - The values of the keys
136
+ */
137
+ getMany<T>(keys: string[]): T[];
138
+ /**
139
+ * Gets the raw value of the key
140
+ * @param {string} key - The key to get the value
141
+ * @returns {CacheableStoreItem | undefined} - The raw value of the key
142
+ */
143
+ getRaw(key: string): CacheableStoreItem | undefined;
144
+ /**
145
+ * Gets the raw values of the keys
146
+ * @param {string[]} keys - The keys to get the values
147
+ * @returns {CacheableStoreItem[]} - The raw values of the keys
148
+ */
149
+ getManyRaw(keys: string[]): Array<CacheableStoreItem | undefined>;
150
+ /**
151
+ * Sets the value of the key
152
+ * @param {string} key - The key to set the value
153
+ * @param {any} value - The value to set
154
+ * @param {number|string|SetOptions} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable.
155
+ * If you want to set expire directly you can do that by setting the expire property in the SetOptions.
156
+ * If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live.
157
+ * @returns {void}
158
+ */
159
+ set(key: string, value: any, ttl?: number | string | SetOptions): void;
160
+ /**
161
+ * Sets the values of the keys
162
+ * @param {CacheableItem[]} items - The items to set
163
+ * @returns {void}
164
+ */
165
+ setMany(items: CacheableItem[]): void;
166
+ /**
167
+ * Checks if the key exists
168
+ * @param {string} key - The key to check
169
+ * @returns {boolean} - If true, the key exists. If false, the key does not exist.
170
+ */
171
+ has(key: string): boolean;
172
+ /**
173
+ * @function hasMany
174
+ * @param {string[]} keys - The keys to check
175
+ * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
176
+ */
177
+ hasMany(keys: string[]): boolean[];
178
+ /**
179
+ * Take will get the key and delete the entry from cache
180
+ * @param {string} key - The key to take
181
+ * @returns {T | undefined} - The value of the key
182
+ */
183
+ take<T>(key: string): T | undefined;
184
+ /**
185
+ * TakeMany will get the keys and delete the entries from cache
186
+ * @param {string[]} keys - The keys to take
187
+ * @returns {T[]} - The values of the keys
188
+ */
189
+ takeMany<T>(keys: string[]): T[];
190
+ /**
191
+ * Delete the key
192
+ * @param {string} key - The key to delete
193
+ * @returns {void}
194
+ */
195
+ delete(key: string): void;
196
+ /**
197
+ * Delete the keys
198
+ * @param {string[]} keys - The keys to delete
199
+ * @returns {void}
200
+ */
201
+ deleteMany(keys: string[]): void;
202
+ /**
203
+ * Clear the cache
204
+ * @returns {void}
205
+ */
206
+ clear(): void;
207
+ /**
208
+ * Get the store based on the key (internal use)
209
+ * @param {string} key - The key to get the store
210
+ * @returns {CacheableHashStore} - The store
211
+ */
212
+ getStore(key: string): Map<string, CacheableStoreItem>;
213
+ /**
214
+ * Hash the key for which store to go to (internal use)
215
+ * @param {string} key - The key to hash
216
+ * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
217
+ * @returns {number} - The hashed key as a number
218
+ */
219
+ getKeyStoreHash(key: string): number;
220
+ /**
221
+ * Clone the value. This is for internal use
222
+ * @param {any} value - The value to clone
223
+ * @returns {any} - The cloned value
224
+ */
225
+ clone(value: any): any;
226
+ /**
227
+ * Add to the front of the LRU cache. This is for internal use
228
+ * @param {string} key - The key to add to the front
229
+ * @returns {void}
230
+ */
231
+ lruAddToFront(key: string): void;
232
+ /**
233
+ * Move to the front of the LRU cache. This is for internal use
234
+ * @param {string} key - The key to move to the front
235
+ * @returns {void}
236
+ */
237
+ lruMoveToFront(key: string): void;
238
+ /**
239
+ * Resize the LRU cache. This is for internal use.
240
+ * @returns {void}
241
+ */
242
+ lruResize(): void;
243
+ /**
244
+ * Check for expiration. This is for internal use
245
+ * @returns {void}
246
+ */
247
+ checkExpiration(): void;
248
+ /**
249
+ * Start the interval check. This is for internal use
250
+ * @returns {void}
251
+ */
252
+ startIntervalCheck(): void;
253
+ /**
254
+ * Stop the interval check. This is for internal use
255
+ * @returns {void}
256
+ */
257
+ stopIntervalCheck(): void;
258
+ /**
259
+ * Wrap the function for caching
260
+ * @param {Function} function_ - The function to wrap
261
+ * @param {Object} [options] - The options to wrap
262
+ * @returns {Function} - The wrapped function
263
+ */
264
+ wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
265
+ private isPrimitive;
266
+ private setTtl;
267
+ private hasExpired;
268
+ }
269
+
270
+ export { CacheableMemory, type CacheableMemoryOptions, type SetOptions, type StoreHashAlgorithmFunction, defaultStoreHashSize, maximumMapSize };
@@ -0,0 +1,270 @@
1
+ import { Hookified } from 'hookified';
2
+ import { WrapFunctionOptions } from '@cacheable/memoize';
3
+ import { HashAlgorithm, CacheableStoreItem, CacheableItem } from '@cacheable/utils';
4
+ export { CacheableItem, CacheableStoreItem, HashAlgorithm, hash, hashToNumber } from '@cacheable/utils';
5
+
6
+ type StoreHashAlgorithmFunction = (key: string, storeHashSize: number) => number;
7
+ /**
8
+ * @typedef {Object} CacheableMemoryOptions
9
+ * @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
10
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
11
+ * undefined then it will not have a time-to-live.
12
+ * @property {boolean} [useClone] - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
13
+ * @property {number} [lruSize] - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
14
+ * @property {number} [checkInterval] - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
15
+ * @property {number} [storeHashSize] - The number of how many Map stores we have for the hash. Default is 10.
16
+ */
17
+ type CacheableMemoryOptions = {
18
+ ttl?: number | string;
19
+ useClone?: boolean;
20
+ lruSize?: number;
21
+ checkInterval?: number;
22
+ storeHashSize?: number;
23
+ storeHashAlgorithm?: HashAlgorithm | ((key: string, storeHashSize: number) => number);
24
+ };
25
+ type SetOptions = {
26
+ ttl?: number | string;
27
+ expire?: number | Date;
28
+ };
29
+ declare const defaultStoreHashSize = 16;
30
+ declare const maximumMapSize = 16777216;
31
+ declare class CacheableMemory extends Hookified {
32
+ private _lru;
33
+ private _storeHashSize;
34
+ private _storeHashAlgorithm;
35
+ private _store;
36
+ private _ttl;
37
+ private _useClone;
38
+ private _lruSize;
39
+ private _checkInterval;
40
+ private _interval;
41
+ /**
42
+ * @constructor
43
+ * @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
44
+ */
45
+ constructor(options?: CacheableMemoryOptions);
46
+ /**
47
+ * Gets the time-to-live
48
+ * @returns {number|string|undefined} - The time-to-live in miliseconds or a human-readable format. If undefined, it will not have a time-to-live.
49
+ */
50
+ get ttl(): number | string | undefined;
51
+ /**
52
+ * Sets the time-to-live
53
+ * @param {number|string|undefined} value - The time-to-live in miliseconds or a human-readable format (example '1s' = 1 second, '1h' = 1 hour). If undefined, it will not have a time-to-live.
54
+ */
55
+ set ttl(value: number | string | undefined);
56
+ /**
57
+ * Gets whether to use clone
58
+ * @returns {boolean} - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
59
+ */
60
+ get useClone(): boolean;
61
+ /**
62
+ * Sets whether to use clone
63
+ * @param {boolean} value - If true, it will clone the value before returning it. If false, it will return the value directly. Default is true.
64
+ */
65
+ set useClone(value: boolean);
66
+ /**
67
+ * Gets the size of the LRU cache
68
+ * @returns {number} - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
69
+ */
70
+ get lruSize(): number;
71
+ /**
72
+ * Sets the size of the LRU cache
73
+ * @param {number} value - The size of the LRU cache. If set to 0, it will not use LRU cache. Default is 0. If you are using LRU then the limit is based on Map() size 17mm.
74
+ */
75
+ set lruSize(value: number);
76
+ /**
77
+ * Gets the check interval
78
+ * @returns {number} - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
79
+ */
80
+ get checkInterval(): number;
81
+ /**
82
+ * Sets the check interval
83
+ * @param {number} value - The interval to check for expired items. If set to 0, it will not check for expired items. Default is 0.
84
+ */
85
+ set checkInterval(value: number);
86
+ /**
87
+ * Gets the size of the cache
88
+ * @returns {number} - The size of the cache
89
+ */
90
+ get size(): number;
91
+ /**
92
+ * Gets the number of hash stores
93
+ * @returns {number} - The number of hash stores
94
+ */
95
+ get storeHashSize(): number;
96
+ /**
97
+ * Sets the number of hash stores. This will recreate the store and all data will be cleared
98
+ * @param {number} value - The number of hash stores
99
+ */
100
+ set storeHashSize(value: number);
101
+ /**
102
+ * Gets the store hash algorithm
103
+ * @returns {HashAlgorithm | StoreHashAlgorithmFunction} - The store hash algorithm
104
+ */
105
+ get storeHashAlgorithm(): HashAlgorithm | StoreHashAlgorithmFunction;
106
+ /**
107
+ * Sets the store hash algorithm. This will recreate the store and all data will be cleared
108
+ * @param {HashAlgorithm | HashAlgorithmFunction} value - The store hash algorithm
109
+ */
110
+ set storeHashAlgorithm(value: HashAlgorithm | StoreHashAlgorithmFunction);
111
+ /**
112
+ * Gets the keys
113
+ * @returns {IterableIterator<string>} - The keys
114
+ */
115
+ get keys(): IterableIterator<string>;
116
+ /**
117
+ * Gets the items
118
+ * @returns {IterableIterator<CacheableStoreItem>} - The items
119
+ */
120
+ get items(): IterableIterator<CacheableStoreItem>;
121
+ /**
122
+ * Gets the store
123
+ * @returns {Array<Map<string, CacheableStoreItem>>} - The store
124
+ */
125
+ get store(): Array<Map<string, CacheableStoreItem>>;
126
+ /**
127
+ * Gets the value of the key
128
+ * @param {string} key - The key to get the value
129
+ * @returns {T | undefined} - The value of the key
130
+ */
131
+ get<T>(key: string): T | undefined;
132
+ /**
133
+ * Gets the values of the keys
134
+ * @param {string[]} keys - The keys to get the values
135
+ * @returns {T[]} - The values of the keys
136
+ */
137
+ getMany<T>(keys: string[]): T[];
138
+ /**
139
+ * Gets the raw value of the key
140
+ * @param {string} key - The key to get the value
141
+ * @returns {CacheableStoreItem | undefined} - The raw value of the key
142
+ */
143
+ getRaw(key: string): CacheableStoreItem | undefined;
144
+ /**
145
+ * Gets the raw values of the keys
146
+ * @param {string[]} keys - The keys to get the values
147
+ * @returns {CacheableStoreItem[]} - The raw values of the keys
148
+ */
149
+ getManyRaw(keys: string[]): Array<CacheableStoreItem | undefined>;
150
+ /**
151
+ * Sets the value of the key
152
+ * @param {string} key - The key to set the value
153
+ * @param {any} value - The value to set
154
+ * @param {number|string|SetOptions} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable.
155
+ * If you want to set expire directly you can do that by setting the expire property in the SetOptions.
156
+ * If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live.
157
+ * @returns {void}
158
+ */
159
+ set(key: string, value: any, ttl?: number | string | SetOptions): void;
160
+ /**
161
+ * Sets the values of the keys
162
+ * @param {CacheableItem[]} items - The items to set
163
+ * @returns {void}
164
+ */
165
+ setMany(items: CacheableItem[]): void;
166
+ /**
167
+ * Checks if the key exists
168
+ * @param {string} key - The key to check
169
+ * @returns {boolean} - If true, the key exists. If false, the key does not exist.
170
+ */
171
+ has(key: string): boolean;
172
+ /**
173
+ * @function hasMany
174
+ * @param {string[]} keys - The keys to check
175
+ * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
176
+ */
177
+ hasMany(keys: string[]): boolean[];
178
+ /**
179
+ * Take will get the key and delete the entry from cache
180
+ * @param {string} key - The key to take
181
+ * @returns {T | undefined} - The value of the key
182
+ */
183
+ take<T>(key: string): T | undefined;
184
+ /**
185
+ * TakeMany will get the keys and delete the entries from cache
186
+ * @param {string[]} keys - The keys to take
187
+ * @returns {T[]} - The values of the keys
188
+ */
189
+ takeMany<T>(keys: string[]): T[];
190
+ /**
191
+ * Delete the key
192
+ * @param {string} key - The key to delete
193
+ * @returns {void}
194
+ */
195
+ delete(key: string): void;
196
+ /**
197
+ * Delete the keys
198
+ * @param {string[]} keys - The keys to delete
199
+ * @returns {void}
200
+ */
201
+ deleteMany(keys: string[]): void;
202
+ /**
203
+ * Clear the cache
204
+ * @returns {void}
205
+ */
206
+ clear(): void;
207
+ /**
208
+ * Get the store based on the key (internal use)
209
+ * @param {string} key - The key to get the store
210
+ * @returns {CacheableHashStore} - The store
211
+ */
212
+ getStore(key: string): Map<string, CacheableStoreItem>;
213
+ /**
214
+ * Hash the key for which store to go to (internal use)
215
+ * @param {string} key - The key to hash
216
+ * Available algorithms are: SHA256, SHA1, MD5, and djb2Hash.
217
+ * @returns {number} - The hashed key as a number
218
+ */
219
+ getKeyStoreHash(key: string): number;
220
+ /**
221
+ * Clone the value. This is for internal use
222
+ * @param {any} value - The value to clone
223
+ * @returns {any} - The cloned value
224
+ */
225
+ clone(value: any): any;
226
+ /**
227
+ * Add to the front of the LRU cache. This is for internal use
228
+ * @param {string} key - The key to add to the front
229
+ * @returns {void}
230
+ */
231
+ lruAddToFront(key: string): void;
232
+ /**
233
+ * Move to the front of the LRU cache. This is for internal use
234
+ * @param {string} key - The key to move to the front
235
+ * @returns {void}
236
+ */
237
+ lruMoveToFront(key: string): void;
238
+ /**
239
+ * Resize the LRU cache. This is for internal use.
240
+ * @returns {void}
241
+ */
242
+ lruResize(): void;
243
+ /**
244
+ * Check for expiration. This is for internal use
245
+ * @returns {void}
246
+ */
247
+ checkExpiration(): void;
248
+ /**
249
+ * Start the interval check. This is for internal use
250
+ * @returns {void}
251
+ */
252
+ startIntervalCheck(): void;
253
+ /**
254
+ * Stop the interval check. This is for internal use
255
+ * @returns {void}
256
+ */
257
+ stopIntervalCheck(): void;
258
+ /**
259
+ * Wrap the function for caching
260
+ * @param {Function} function_ - The function to wrap
261
+ * @param {Object} [options] - The options to wrap
262
+ * @returns {Function} - The wrapped function
263
+ */
264
+ wrap<T, Arguments extends any[]>(function_: (...arguments_: Arguments) => T, options?: WrapFunctionOptions): (...arguments_: Arguments) => T;
265
+ private isPrimitive;
266
+ private setTtl;
267
+ private hasExpired;
268
+ }
269
+
270
+ export { CacheableMemory, type CacheableMemoryOptions, type SetOptions, type StoreHashAlgorithmFunction, defaultStoreHashSize, maximumMapSize };