flat-cache 5.0.0 → 6.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,226 @@
1
+ import { CacheableMemory } from 'cacheable';
2
+
3
+ type FlatCacheOptions = {
4
+ ttl?: number | string;
5
+ useClone?: boolean;
6
+ lruSize?: number;
7
+ expirationInterval?: number;
8
+ persistInterval?: number;
9
+ cacheDir?: string;
10
+ cacheId?: string;
11
+ };
12
+ declare class FlatCache {
13
+ private readonly _cache;
14
+ private _cacheDir;
15
+ private _cacheId;
16
+ private _persistInterval;
17
+ private _persistTimer;
18
+ constructor(options?: FlatCacheOptions);
19
+ /**
20
+ * The cache object
21
+ * @property cache
22
+ * @type {CacheableMemory}
23
+ */
24
+ get cache(): CacheableMemory;
25
+ /**
26
+ * The cache directory
27
+ * @property cacheDir
28
+ * @type {String}
29
+ * @default '.cache'
30
+ */
31
+ get cacheDir(): string;
32
+ /**
33
+ * Set the cache directory
34
+ * @property cacheDir
35
+ * @type {String}
36
+ * @default '.cache'
37
+ */
38
+ set cacheDir(value: string);
39
+ /**
40
+ * The cache id
41
+ * @property cacheId
42
+ * @type {String}
43
+ * @default 'cache1'
44
+ */
45
+ get cacheId(): string;
46
+ /**
47
+ * Set the cache id
48
+ * @property cacheId
49
+ * @type {String}
50
+ * @default 'cache1'
51
+ */
52
+ set cacheId(value: string);
53
+ /**
54
+ * The interval to persist the cache to disk. 0 means no timed persistence
55
+ * @property persistInterval
56
+ * @type {Number}
57
+ * @default 0
58
+ */
59
+ get persistInterval(): number;
60
+ /**
61
+ * Set the interval to persist the cache to disk. 0 means no timed persistence
62
+ * @property persistInterval
63
+ * @type {Number}
64
+ * @default 0
65
+ */
66
+ set persistInterval(value: number);
67
+ /**
68
+ * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
69
+ * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
70
+ * then the cache module directory `.cacheDir` will be used instead
71
+ *
72
+ * @method load
73
+ * @param docId {String} the id of the cache, would also be used as the name of the file cache
74
+ * @param [cacheDir] {String} directory for the cache entry
75
+ */
76
+ load(documentId: string, cacheDir?: string): void;
77
+ /**
78
+ * Load the cache from the provided file
79
+ * @method loadFile
80
+ * @param {String} pathToFile the path to the file containing the info for the cache
81
+ */
82
+ loadFile(pathToFile: string): void;
83
+ /**
84
+ * Returns the entire persisted object
85
+ * @method all
86
+ * @returns {*}
87
+ */
88
+ all(): Record<string, any>;
89
+ /**
90
+ * Returns an array with all the items in the cache { key, value, ttl }
91
+ * @method items
92
+ * @returns {Array}
93
+ */
94
+ get items(): {
95
+ key: string;
96
+ value: any;
97
+ expires?: number;
98
+ }[];
99
+ /**
100
+ * Returns the path to the file where the cache is persisted
101
+ * @method cacheFilePath
102
+ * @returns {String}
103
+ */
104
+ get cacheFilePath(): string;
105
+ /**
106
+ * Returns the path to the cache directory
107
+ * @method cacheDirPath
108
+ * @returns {String}
109
+ */
110
+ get cacheDirPath(): string;
111
+ /**
112
+ * Returns an array with all the keys in the cache
113
+ * @method keys
114
+ * @returns {Array}
115
+ */
116
+ keys(): string[];
117
+ /**
118
+ * (Legacy) set key method. This method will be deprecated in the future
119
+ * @method setKey
120
+ * @param key {string} the key to set
121
+ * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
122
+ */
123
+ setKey(key: string, value: any, ttl?: number | string): void;
124
+ /**
125
+ * Sets a key to a given value
126
+ * @method set
127
+ * @param key {string} the key to set
128
+ * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
129
+ * @param [ttl] {number} the time to live in milliseconds
130
+ */
131
+ set(key: string, value: any, ttl?: number | string): void;
132
+ /**
133
+ * (Legacy) Remove a given key from the cache. This method will be deprecated in the future
134
+ * @method removeKey
135
+ * @param key {String} the key to remove from the object
136
+ */
137
+ removeKey(key: string): void;
138
+ /**
139
+ * Remove a given key from the cache
140
+ * @method delete
141
+ * @param key {String} the key to remove from the object
142
+ */
143
+ delete(key: string): void;
144
+ /**
145
+ * (Legacy) Return the value of the provided key. This method will be deprecated in the future
146
+ * @method getKey<T>
147
+ * @param key {String} the name of the key to retrieve
148
+ * @returns {*} at T the value from the key
149
+ */
150
+ getKey<T>(key: string): T;
151
+ /**
152
+ * Return the value of the provided key
153
+ * @method get<T>
154
+ * @param key {String} the name of the key to retrieve
155
+ * @returns {*} at T the value from the key
156
+ */
157
+ get<T>(key: string): T;
158
+ /**
159
+ * Clear the cache
160
+ * @method clear
161
+ */
162
+ clear(): void;
163
+ /**
164
+ * Save the state of the cache identified by the docId to disk
165
+ * as a JSON structure
166
+ * @method save
167
+ */
168
+ save(): void;
169
+ /**
170
+ * Remove the file where the cache is persisted
171
+ * @method removeCacheFile
172
+ * @return {Boolean} true or false if the file was successfully deleted
173
+ */
174
+ removeCacheFile(): boolean;
175
+ /**
176
+ * Destroy the cache. This will remove the directory, file, and memory cache
177
+ * @method destroy
178
+ * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed
179
+ * @return {undefined}
180
+ */
181
+ destroy(includeCacheDirectory?: boolean): void;
182
+ /**
183
+ * Start the auto persist interval
184
+ * @method startAutoPersist
185
+ */
186
+ startAutoPersist(): void;
187
+ /**
188
+ * Stop the auto persist interval
189
+ * @method stopAutoPersist
190
+ */
191
+ stopAutoPersist(): void;
192
+ }
193
+ /**
194
+ * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
195
+ * cache storage.
196
+ *
197
+ * @method create
198
+ * @param docId {String} the id of the cache, would also be used as the name of the file cache
199
+ * @param cacheDirectory {String} directory for the cache entry
200
+ * @param options {FlatCacheOptions} options for the cache
201
+ * @returns {cache} cache instance
202
+ */
203
+ declare function create(documentId: string, cacheDirectory?: string, options?: FlatCacheOptions): FlatCache;
204
+ /**
205
+ * Load a cache from the provided file
206
+ * @method createFromFile
207
+ * @param {String} filePath the path to the file containing the info for the cache
208
+ * @param options {FlatCacheOptions} options for the cache
209
+ * @returns {cache} cache instance
210
+ */
211
+ declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache;
212
+ /**
213
+ * Clear the cache identified by the given Id
214
+ * @method clearCacheById
215
+ * @param cacheId {String} the id of the cache
216
+ * @param cacheDirectory {String} directory for the cache entry
217
+ */
218
+ declare function clearCacheById(cacheId: string, cacheDirectory?: string): void;
219
+ /**
220
+ * Clear the cache directory
221
+ * @method clearAll
222
+ * @param cacheDir {String} directory for the cache entry
223
+ */
224
+ declare function clearAll(cacheDirectory?: string): void;
225
+
226
+ export { FlatCache, type FlatCacheOptions, clearAll, clearCacheById, create, createFromFile };
@@ -0,0 +1,226 @@
1
+ import { CacheableMemory } from 'cacheable';
2
+
3
+ type FlatCacheOptions = {
4
+ ttl?: number | string;
5
+ useClone?: boolean;
6
+ lruSize?: number;
7
+ expirationInterval?: number;
8
+ persistInterval?: number;
9
+ cacheDir?: string;
10
+ cacheId?: string;
11
+ };
12
+ declare class FlatCache {
13
+ private readonly _cache;
14
+ private _cacheDir;
15
+ private _cacheId;
16
+ private _persistInterval;
17
+ private _persistTimer;
18
+ constructor(options?: FlatCacheOptions);
19
+ /**
20
+ * The cache object
21
+ * @property cache
22
+ * @type {CacheableMemory}
23
+ */
24
+ get cache(): CacheableMemory;
25
+ /**
26
+ * The cache directory
27
+ * @property cacheDir
28
+ * @type {String}
29
+ * @default '.cache'
30
+ */
31
+ get cacheDir(): string;
32
+ /**
33
+ * Set the cache directory
34
+ * @property cacheDir
35
+ * @type {String}
36
+ * @default '.cache'
37
+ */
38
+ set cacheDir(value: string);
39
+ /**
40
+ * The cache id
41
+ * @property cacheId
42
+ * @type {String}
43
+ * @default 'cache1'
44
+ */
45
+ get cacheId(): string;
46
+ /**
47
+ * Set the cache id
48
+ * @property cacheId
49
+ * @type {String}
50
+ * @default 'cache1'
51
+ */
52
+ set cacheId(value: string);
53
+ /**
54
+ * The interval to persist the cache to disk. 0 means no timed persistence
55
+ * @property persistInterval
56
+ * @type {Number}
57
+ * @default 0
58
+ */
59
+ get persistInterval(): number;
60
+ /**
61
+ * Set the interval to persist the cache to disk. 0 means no timed persistence
62
+ * @property persistInterval
63
+ * @type {Number}
64
+ * @default 0
65
+ */
66
+ set persistInterval(value: number);
67
+ /**
68
+ * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
69
+ * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
70
+ * then the cache module directory `.cacheDir` will be used instead
71
+ *
72
+ * @method load
73
+ * @param docId {String} the id of the cache, would also be used as the name of the file cache
74
+ * @param [cacheDir] {String} directory for the cache entry
75
+ */
76
+ load(documentId: string, cacheDir?: string): void;
77
+ /**
78
+ * Load the cache from the provided file
79
+ * @method loadFile
80
+ * @param {String} pathToFile the path to the file containing the info for the cache
81
+ */
82
+ loadFile(pathToFile: string): void;
83
+ /**
84
+ * Returns the entire persisted object
85
+ * @method all
86
+ * @returns {*}
87
+ */
88
+ all(): Record<string, any>;
89
+ /**
90
+ * Returns an array with all the items in the cache { key, value, ttl }
91
+ * @method items
92
+ * @returns {Array}
93
+ */
94
+ get items(): {
95
+ key: string;
96
+ value: any;
97
+ expires?: number;
98
+ }[];
99
+ /**
100
+ * Returns the path to the file where the cache is persisted
101
+ * @method cacheFilePath
102
+ * @returns {String}
103
+ */
104
+ get cacheFilePath(): string;
105
+ /**
106
+ * Returns the path to the cache directory
107
+ * @method cacheDirPath
108
+ * @returns {String}
109
+ */
110
+ get cacheDirPath(): string;
111
+ /**
112
+ * Returns an array with all the keys in the cache
113
+ * @method keys
114
+ * @returns {Array}
115
+ */
116
+ keys(): string[];
117
+ /**
118
+ * (Legacy) set key method. This method will be deprecated in the future
119
+ * @method setKey
120
+ * @param key {string} the key to set
121
+ * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
122
+ */
123
+ setKey(key: string, value: any, ttl?: number | string): void;
124
+ /**
125
+ * Sets a key to a given value
126
+ * @method set
127
+ * @param key {string} the key to set
128
+ * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
129
+ * @param [ttl] {number} the time to live in milliseconds
130
+ */
131
+ set(key: string, value: any, ttl?: number | string): void;
132
+ /**
133
+ * (Legacy) Remove a given key from the cache. This method will be deprecated in the future
134
+ * @method removeKey
135
+ * @param key {String} the key to remove from the object
136
+ */
137
+ removeKey(key: string): void;
138
+ /**
139
+ * Remove a given key from the cache
140
+ * @method delete
141
+ * @param key {String} the key to remove from the object
142
+ */
143
+ delete(key: string): void;
144
+ /**
145
+ * (Legacy) Return the value of the provided key. This method will be deprecated in the future
146
+ * @method getKey<T>
147
+ * @param key {String} the name of the key to retrieve
148
+ * @returns {*} at T the value from the key
149
+ */
150
+ getKey<T>(key: string): T;
151
+ /**
152
+ * Return the value of the provided key
153
+ * @method get<T>
154
+ * @param key {String} the name of the key to retrieve
155
+ * @returns {*} at T the value from the key
156
+ */
157
+ get<T>(key: string): T;
158
+ /**
159
+ * Clear the cache
160
+ * @method clear
161
+ */
162
+ clear(): void;
163
+ /**
164
+ * Save the state of the cache identified by the docId to disk
165
+ * as a JSON structure
166
+ * @method save
167
+ */
168
+ save(): void;
169
+ /**
170
+ * Remove the file where the cache is persisted
171
+ * @method removeCacheFile
172
+ * @return {Boolean} true or false if the file was successfully deleted
173
+ */
174
+ removeCacheFile(): boolean;
175
+ /**
176
+ * Destroy the cache. This will remove the directory, file, and memory cache
177
+ * @method destroy
178
+ * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed
179
+ * @return {undefined}
180
+ */
181
+ destroy(includeCacheDirectory?: boolean): void;
182
+ /**
183
+ * Start the auto persist interval
184
+ * @method startAutoPersist
185
+ */
186
+ startAutoPersist(): void;
187
+ /**
188
+ * Stop the auto persist interval
189
+ * @method stopAutoPersist
190
+ */
191
+ stopAutoPersist(): void;
192
+ }
193
+ /**
194
+ * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
195
+ * cache storage.
196
+ *
197
+ * @method create
198
+ * @param docId {String} the id of the cache, would also be used as the name of the file cache
199
+ * @param cacheDirectory {String} directory for the cache entry
200
+ * @param options {FlatCacheOptions} options for the cache
201
+ * @returns {cache} cache instance
202
+ */
203
+ declare function create(documentId: string, cacheDirectory?: string, options?: FlatCacheOptions): FlatCache;
204
+ /**
205
+ * Load a cache from the provided file
206
+ * @method createFromFile
207
+ * @param {String} filePath the path to the file containing the info for the cache
208
+ * @param options {FlatCacheOptions} options for the cache
209
+ * @returns {cache} cache instance
210
+ */
211
+ declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache;
212
+ /**
213
+ * Clear the cache identified by the given Id
214
+ * @method clearCacheById
215
+ * @param cacheId {String} the id of the cache
216
+ * @param cacheDirectory {String} directory for the cache entry
217
+ */
218
+ declare function clearCacheById(cacheId: string, cacheDirectory?: string): void;
219
+ /**
220
+ * Clear the cache directory
221
+ * @method clearAll
222
+ * @param cacheDir {String} directory for the cache entry
223
+ */
224
+ declare function clearAll(cacheDirectory?: string): void;
225
+
226
+ export { FlatCache, type FlatCacheOptions, clearAll, clearCacheById, create, createFromFile };