flat-cache 6.1.22 → 6.1.23
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.
- package/dist/index.cjs +466 -476
- package/dist/index.d.cts +215 -214
- package/dist/index.d.mts +256 -0
- package/dist/index.mjs +451 -0
- package/package.json +9 -9
- package/dist/index.d.ts +0 -255
- package/dist/index.js +0 -455
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { CacheableMemory } from "cacheable";
|
|
2
|
+
import { Hookified } from "hookified";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
type FlatCacheOptions = {
|
|
6
|
+
ttl?: number | string;
|
|
7
|
+
useClone?: boolean;
|
|
8
|
+
lruSize?: number;
|
|
9
|
+
expirationInterval?: number;
|
|
10
|
+
persistInterval?: number;
|
|
11
|
+
cacheDir?: string;
|
|
12
|
+
cacheId?: string;
|
|
13
|
+
deserialize?: (data: string) => any;
|
|
14
|
+
serialize?: (data: any) => string;
|
|
15
|
+
};
|
|
16
|
+
declare enum FlatCacheEvents {
|
|
17
|
+
SAVE = "save",
|
|
18
|
+
LOAD = "load",
|
|
19
|
+
DELETE = "delete",
|
|
20
|
+
CLEAR = "clear",
|
|
21
|
+
DESTROY = "destroy",
|
|
22
|
+
ERROR = "error",
|
|
23
|
+
EXPIRED = "expired"
|
|
24
|
+
}
|
|
25
|
+
declare class FlatCache extends Hookified {
|
|
26
|
+
private readonly _cache;
|
|
27
|
+
private _cacheDir;
|
|
28
|
+
private _cacheId;
|
|
29
|
+
private _persistInterval;
|
|
30
|
+
private _persistTimer;
|
|
31
|
+
private _changesSinceLastSave;
|
|
32
|
+
private readonly _parse;
|
|
33
|
+
private readonly _stringify;
|
|
34
|
+
constructor(options?: FlatCacheOptions);
|
|
35
|
+
/**
|
|
36
|
+
* The cache object
|
|
37
|
+
* @property cache
|
|
38
|
+
* @type {CacheableMemory}
|
|
39
|
+
*/
|
|
40
|
+
get cache(): CacheableMemory;
|
|
41
|
+
/**
|
|
42
|
+
* The cache directory
|
|
43
|
+
* @property cacheDir
|
|
44
|
+
* @type {String}
|
|
45
|
+
* @default '.cache'
|
|
46
|
+
*/
|
|
47
|
+
get cacheDir(): string;
|
|
48
|
+
/**
|
|
49
|
+
* Set the cache directory
|
|
50
|
+
* @property cacheDir
|
|
51
|
+
* @type {String}
|
|
52
|
+
* @default '.cache'
|
|
53
|
+
*/
|
|
54
|
+
set cacheDir(value: string);
|
|
55
|
+
/**
|
|
56
|
+
* The cache id
|
|
57
|
+
* @property cacheId
|
|
58
|
+
* @type {String}
|
|
59
|
+
* @default 'cache1'
|
|
60
|
+
*/
|
|
61
|
+
get cacheId(): string;
|
|
62
|
+
/**
|
|
63
|
+
* Set the cache id
|
|
64
|
+
* @property cacheId
|
|
65
|
+
* @type {String}
|
|
66
|
+
* @default 'cache1'
|
|
67
|
+
*/
|
|
68
|
+
set cacheId(value: string);
|
|
69
|
+
/**
|
|
70
|
+
* The flag to indicate if there are changes since the last save
|
|
71
|
+
* @property changesSinceLastSave
|
|
72
|
+
* @type {Boolean}
|
|
73
|
+
* @default false
|
|
74
|
+
*/
|
|
75
|
+
get changesSinceLastSave(): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* The interval to persist the cache to disk. 0 means no timed persistence
|
|
78
|
+
* @property persistInterval
|
|
79
|
+
* @type {Number}
|
|
80
|
+
* @default 0
|
|
81
|
+
*/
|
|
82
|
+
get persistInterval(): number;
|
|
83
|
+
/**
|
|
84
|
+
* Set the interval to persist the cache to disk. 0 means no timed persistence
|
|
85
|
+
* @property persistInterval
|
|
86
|
+
* @type {Number}
|
|
87
|
+
* @default 0
|
|
88
|
+
*/
|
|
89
|
+
set persistInterval(value: number);
|
|
90
|
+
/**
|
|
91
|
+
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
|
92
|
+
* cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
|
|
93
|
+
* then the cache module directory `.cacheDir` will be used instead
|
|
94
|
+
*
|
|
95
|
+
* @method load
|
|
96
|
+
* @param cacheId {String} the id of the cache, would also be used as the name of the file cache
|
|
97
|
+
* @param cacheDir {String} directory for the cache entry
|
|
98
|
+
*/
|
|
99
|
+
load(cacheId?: string, cacheDir?: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Load the cache from the provided file
|
|
102
|
+
* @method loadFile
|
|
103
|
+
* @param {String} pathToFile the path to the file containing the info for the cache
|
|
104
|
+
*/
|
|
105
|
+
loadFile(pathToFile: string): void;
|
|
106
|
+
loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void;
|
|
107
|
+
/**
|
|
108
|
+
* Returns the entire persisted object
|
|
109
|
+
* @method all
|
|
110
|
+
* @returns {*}
|
|
111
|
+
*/
|
|
112
|
+
all(): Record<string, any>;
|
|
113
|
+
/**
|
|
114
|
+
* Returns an array with all the items in the cache { key, value, expires }
|
|
115
|
+
* @method items
|
|
116
|
+
* @returns {Array}
|
|
117
|
+
*/
|
|
118
|
+
get items(): Array<{
|
|
119
|
+
key: string;
|
|
120
|
+
value: any;
|
|
121
|
+
expires?: number;
|
|
122
|
+
}>;
|
|
123
|
+
/**
|
|
124
|
+
* Returns the path to the file where the cache is persisted
|
|
125
|
+
* @method cacheFilePath
|
|
126
|
+
* @returns {String}
|
|
127
|
+
*/
|
|
128
|
+
get cacheFilePath(): string;
|
|
129
|
+
/**
|
|
130
|
+
* Returns the path to the cache directory
|
|
131
|
+
* @method cacheDirPath
|
|
132
|
+
* @returns {String}
|
|
133
|
+
*/
|
|
134
|
+
get cacheDirPath(): string;
|
|
135
|
+
/**
|
|
136
|
+
* Returns an array with all the keys in the cache
|
|
137
|
+
* @method keys
|
|
138
|
+
* @returns {Array}
|
|
139
|
+
*/
|
|
140
|
+
keys(): string[];
|
|
141
|
+
/**
|
|
142
|
+
* (Legacy) set key method. This method will be deprecated in the future
|
|
143
|
+
* @method setKey
|
|
144
|
+
* @param key {string} the key to set
|
|
145
|
+
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
146
|
+
*/
|
|
147
|
+
setKey(key: string, value: any, ttl?: number | string): void;
|
|
148
|
+
/**
|
|
149
|
+
* Sets a key to a given value
|
|
150
|
+
* @method set
|
|
151
|
+
* @param key {string} the key to set
|
|
152
|
+
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
153
|
+
* @param [ttl] {number} the time to live in milliseconds
|
|
154
|
+
*/
|
|
155
|
+
set(key: string, value: any, ttl?: number | string): void;
|
|
156
|
+
/**
|
|
157
|
+
* (Legacy) Remove a given key from the cache. This method will be deprecated in the future
|
|
158
|
+
* @method removeKey
|
|
159
|
+
* @param key {String} the key to remove from the object
|
|
160
|
+
*/
|
|
161
|
+
removeKey(key: string): void;
|
|
162
|
+
/**
|
|
163
|
+
* Remove a given key from the cache
|
|
164
|
+
* @method delete
|
|
165
|
+
* @param key {String} the key to remove from the object
|
|
166
|
+
*/
|
|
167
|
+
delete(key: string): void;
|
|
168
|
+
/**
|
|
169
|
+
* (Legacy) Return the value of the provided key. This method will be deprecated in the future
|
|
170
|
+
* @method getKey<T>
|
|
171
|
+
* @param key {String} the name of the key to retrieve
|
|
172
|
+
* @returns {*} at T the value from the key
|
|
173
|
+
*/
|
|
174
|
+
getKey<T>(key: string): T;
|
|
175
|
+
/**
|
|
176
|
+
* Return the value of the provided key
|
|
177
|
+
* @method get<T>
|
|
178
|
+
* @param key {String} the name of the key to retrieve
|
|
179
|
+
* @returns {*} at T the value from the key
|
|
180
|
+
*/
|
|
181
|
+
get<T>(key: string): T;
|
|
182
|
+
/**
|
|
183
|
+
* Clear the cache and save the state to disk
|
|
184
|
+
* @method clear
|
|
185
|
+
*/
|
|
186
|
+
clear(): void;
|
|
187
|
+
/**
|
|
188
|
+
* Save the state of the cache identified by the docId to disk
|
|
189
|
+
* as a JSON structure
|
|
190
|
+
* @method save
|
|
191
|
+
*/
|
|
192
|
+
save(force?: boolean): void;
|
|
193
|
+
/**
|
|
194
|
+
* Remove the file where the cache is persisted
|
|
195
|
+
* @method removeCacheFile
|
|
196
|
+
* @return {Boolean} true or false if the file was successfully deleted
|
|
197
|
+
*/
|
|
198
|
+
removeCacheFile(): boolean;
|
|
199
|
+
/**
|
|
200
|
+
* Destroy the cache. This will remove the directory, file, and memory cache
|
|
201
|
+
* @method destroy
|
|
202
|
+
* @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed
|
|
203
|
+
* @return {undefined}
|
|
204
|
+
*/
|
|
205
|
+
destroy(includeCacheDirectory?: boolean): void;
|
|
206
|
+
/**
|
|
207
|
+
* Start the auto persist interval
|
|
208
|
+
* @method startAutoPersist
|
|
209
|
+
*/
|
|
210
|
+
startAutoPersist(): void;
|
|
211
|
+
/**
|
|
212
|
+
* Stop the auto persist interval
|
|
213
|
+
* @method stopAutoPersist
|
|
214
|
+
*/
|
|
215
|
+
stopAutoPersist(): void;
|
|
216
|
+
}
|
|
217
|
+
declare class FlatCacheDefault {
|
|
218
|
+
static create: typeof create;
|
|
219
|
+
static createFromFile: typeof createFromFile;
|
|
220
|
+
static clearCacheById: typeof clearCacheById;
|
|
221
|
+
static clearAll: typeof clearAll;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
|
225
|
+
* cache storage.
|
|
226
|
+
*
|
|
227
|
+
* @method create
|
|
228
|
+
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
|
229
|
+
* @param cacheDirectory {String} directory for the cache entry
|
|
230
|
+
* @param options {FlatCacheOptions} options for the cache
|
|
231
|
+
* @returns {cache} cache instance
|
|
232
|
+
*/
|
|
233
|
+
declare function create(options?: FlatCacheOptions): FlatCache;
|
|
234
|
+
/**
|
|
235
|
+
* Load a cache from the provided file
|
|
236
|
+
* @method createFromFile
|
|
237
|
+
* @param {String} filePath the path to the file containing the info for the cache
|
|
238
|
+
* @param options {FlatCacheOptions} options for the cache
|
|
239
|
+
* @returns {cache} cache instance
|
|
240
|
+
*/
|
|
241
|
+
declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache;
|
|
242
|
+
/**
|
|
243
|
+
* Clear the cache identified by the given Id. This will only remove the cache from disk.
|
|
244
|
+
* @method clearCacheById
|
|
245
|
+
* @param cacheId {String} the id of the cache
|
|
246
|
+
* @param cacheDirectory {String} directory for the cache entry
|
|
247
|
+
*/
|
|
248
|
+
declare function clearCacheById(cacheId: string, cacheDirectory?: string): void;
|
|
249
|
+
/**
|
|
250
|
+
* Clear the cache directory
|
|
251
|
+
* @method clearAll
|
|
252
|
+
* @param cacheDir {String} directory for the cache entry
|
|
253
|
+
*/
|
|
254
|
+
declare function clearAll(cacheDirectory?: string): void;
|
|
255
|
+
//#endregion
|
|
256
|
+
export { FlatCache, FlatCacheEvents, FlatCacheOptions, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { CacheableMemory } from "cacheable";
|
|
4
|
+
import { parse, stringify } from "flatted";
|
|
5
|
+
import { Hookified } from "hookified";
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
let FlatCacheEvents = /* @__PURE__ */ function(FlatCacheEvents) {
|
|
8
|
+
FlatCacheEvents["SAVE"] = "save";
|
|
9
|
+
FlatCacheEvents["LOAD"] = "load";
|
|
10
|
+
FlatCacheEvents["DELETE"] = "delete";
|
|
11
|
+
FlatCacheEvents["CLEAR"] = "clear";
|
|
12
|
+
FlatCacheEvents["DESTROY"] = "destroy";
|
|
13
|
+
FlatCacheEvents["ERROR"] = "error";
|
|
14
|
+
FlatCacheEvents["EXPIRED"] = "expired";
|
|
15
|
+
return FlatCacheEvents;
|
|
16
|
+
}({});
|
|
17
|
+
var FlatCache = class extends Hookified {
|
|
18
|
+
_cache = new CacheableMemory();
|
|
19
|
+
_cacheDir = ".cache";
|
|
20
|
+
_cacheId = "cache1";
|
|
21
|
+
_persistInterval = 0;
|
|
22
|
+
_persistTimer;
|
|
23
|
+
_changesSinceLastSave = false;
|
|
24
|
+
_parse = parse;
|
|
25
|
+
_stringify = stringify;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
super();
|
|
28
|
+
if (options) this._cache = new CacheableMemory({
|
|
29
|
+
ttl: options.ttl,
|
|
30
|
+
useClone: options.useClone,
|
|
31
|
+
lruSize: options.lruSize,
|
|
32
|
+
checkInterval: options.expirationInterval
|
|
33
|
+
});
|
|
34
|
+
if (options?.cacheDir) this._cacheDir = options.cacheDir;
|
|
35
|
+
if (options?.cacheId) this._cacheId = options.cacheId;
|
|
36
|
+
if (options?.persistInterval) {
|
|
37
|
+
this._persistInterval = options.persistInterval;
|
|
38
|
+
this.startAutoPersist();
|
|
39
|
+
}
|
|
40
|
+
if (options?.deserialize) this._parse = options.deserialize;
|
|
41
|
+
if (options?.serialize) this._stringify = options.serialize;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The cache object
|
|
45
|
+
* @property cache
|
|
46
|
+
* @type {CacheableMemory}
|
|
47
|
+
*/
|
|
48
|
+
get cache() {
|
|
49
|
+
return this._cache;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The cache directory
|
|
53
|
+
* @property cacheDir
|
|
54
|
+
* @type {String}
|
|
55
|
+
* @default '.cache'
|
|
56
|
+
*/
|
|
57
|
+
get cacheDir() {
|
|
58
|
+
return this._cacheDir;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Set the cache directory
|
|
62
|
+
* @property cacheDir
|
|
63
|
+
* @type {String}
|
|
64
|
+
* @default '.cache'
|
|
65
|
+
*/
|
|
66
|
+
set cacheDir(value) {
|
|
67
|
+
this._cacheDir = value;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The cache id
|
|
71
|
+
* @property cacheId
|
|
72
|
+
* @type {String}
|
|
73
|
+
* @default 'cache1'
|
|
74
|
+
*/
|
|
75
|
+
get cacheId() {
|
|
76
|
+
return this._cacheId;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Set the cache id
|
|
80
|
+
* @property cacheId
|
|
81
|
+
* @type {String}
|
|
82
|
+
* @default 'cache1'
|
|
83
|
+
*/
|
|
84
|
+
set cacheId(value) {
|
|
85
|
+
this._cacheId = value;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The flag to indicate if there are changes since the last save
|
|
89
|
+
* @property changesSinceLastSave
|
|
90
|
+
* @type {Boolean}
|
|
91
|
+
* @default false
|
|
92
|
+
*/
|
|
93
|
+
get changesSinceLastSave() {
|
|
94
|
+
return this._changesSinceLastSave;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The interval to persist the cache to disk. 0 means no timed persistence
|
|
98
|
+
* @property persistInterval
|
|
99
|
+
* @type {Number}
|
|
100
|
+
* @default 0
|
|
101
|
+
*/
|
|
102
|
+
get persistInterval() {
|
|
103
|
+
return this._persistInterval;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Set the interval to persist the cache to disk. 0 means no timed persistence
|
|
107
|
+
* @property persistInterval
|
|
108
|
+
* @type {Number}
|
|
109
|
+
* @default 0
|
|
110
|
+
*/
|
|
111
|
+
set persistInterval(value) {
|
|
112
|
+
this._persistInterval = value;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
|
116
|
+
* cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
|
|
117
|
+
* then the cache module directory `.cacheDir` will be used instead
|
|
118
|
+
*
|
|
119
|
+
* @method load
|
|
120
|
+
* @param cacheId {String} the id of the cache, would also be used as the name of the file cache
|
|
121
|
+
* @param cacheDir {String} directory for the cache entry
|
|
122
|
+
*/
|
|
123
|
+
load(cacheId, cacheDir) {
|
|
124
|
+
try {
|
|
125
|
+
const filePath = path.resolve(`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`);
|
|
126
|
+
this.loadFile(filePath);
|
|
127
|
+
this.emit("load");
|
|
128
|
+
} catch (error) {
|
|
129
|
+
/* v8 ignore next -- @preserve */
|
|
130
|
+
this.emit("error", error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Load the cache from the provided file
|
|
135
|
+
* @method loadFile
|
|
136
|
+
* @param {String} pathToFile the path to the file containing the info for the cache
|
|
137
|
+
*/
|
|
138
|
+
loadFile(pathToFile) {
|
|
139
|
+
if (fs.existsSync(pathToFile)) {
|
|
140
|
+
const data = fs.readFileSync(pathToFile, "utf8");
|
|
141
|
+
const items = this._parse(data);
|
|
142
|
+
if (Array.isArray(items)) {
|
|
143
|
+
for (const item of items) if (item && typeof item === "object" && "key" in item) if (item.expires) this._cache.set(item.key, item.value, { expire: item.expires });
|
|
144
|
+
else if (item.timestamp)
|
|
145
|
+
/* v8 ignore next -- @preserve */
|
|
146
|
+
this._cache.set(item.key, item.value, { expire: item.timestamp });
|
|
147
|
+
else this._cache.set(item.key, item.value);
|
|
148
|
+
} else for (const key of Object.keys(items)) {
|
|
149
|
+
const item = items[key];
|
|
150
|
+
/* v8 ignore next -- @preserve */
|
|
151
|
+
if (item && typeof item === "object" && "key" in item) this._cache.set(item.key, item.value, { expire: item.expires });
|
|
152
|
+
else if (item && typeof item === "object" && item.timestamp)
|
|
153
|
+
/* v8 ignore next -- @preserve */
|
|
154
|
+
this._cache.set(key, item, { expire: item.timestamp });
|
|
155
|
+
else this._cache.set(key, item);
|
|
156
|
+
}
|
|
157
|
+
this._changesSinceLastSave = true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
loadFileStream(pathToFile, onProgress, onEnd, onError) {
|
|
161
|
+
if (fs.existsSync(pathToFile)) {
|
|
162
|
+
const total = fs.statSync(pathToFile).size;
|
|
163
|
+
let loaded = 0;
|
|
164
|
+
let streamData = "";
|
|
165
|
+
const readStream = fs.createReadStream(pathToFile, { encoding: "utf8" });
|
|
166
|
+
readStream.on("data", (chunk) => {
|
|
167
|
+
loaded += chunk.length;
|
|
168
|
+
streamData += chunk;
|
|
169
|
+
onProgress(loaded, total);
|
|
170
|
+
});
|
|
171
|
+
readStream.on("end", () => {
|
|
172
|
+
const items = this._parse(streamData);
|
|
173
|
+
for (const key of Object.keys(items)) this._cache.set(items[key].key, items[key].value, { expire: items[key].expires });
|
|
174
|
+
this._changesSinceLastSave = true;
|
|
175
|
+
onEnd();
|
|
176
|
+
});
|
|
177
|
+
/* v8 ignore next -- @preserve */
|
|
178
|
+
readStream.on("error", (error) => {
|
|
179
|
+
this.emit("error", error);
|
|
180
|
+
if (onError) onError(error);
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
const error = /* @__PURE__ */ new Error(`Cache file ${pathToFile} does not exist`);
|
|
184
|
+
this.emit("error", error);
|
|
185
|
+
/* v8 ignore next -- @preserve */
|
|
186
|
+
if (onError) onError(error);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns the entire persisted object
|
|
191
|
+
* @method all
|
|
192
|
+
* @returns {*}
|
|
193
|
+
*/
|
|
194
|
+
all() {
|
|
195
|
+
const result = {};
|
|
196
|
+
const items = [...this._cache.items];
|
|
197
|
+
for (const item of items) result[item.key] = item.value;
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Returns an array with all the items in the cache { key, value, expires }
|
|
202
|
+
* @method items
|
|
203
|
+
* @returns {Array}
|
|
204
|
+
*/
|
|
205
|
+
get items() {
|
|
206
|
+
return [...this._cache.items];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Returns the path to the file where the cache is persisted
|
|
210
|
+
* @method cacheFilePath
|
|
211
|
+
* @returns {String}
|
|
212
|
+
*/
|
|
213
|
+
get cacheFilePath() {
|
|
214
|
+
return path.resolve(`${this._cacheDir}/${this._cacheId}`);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Returns the path to the cache directory
|
|
218
|
+
* @method cacheDirPath
|
|
219
|
+
* @returns {String}
|
|
220
|
+
*/
|
|
221
|
+
get cacheDirPath() {
|
|
222
|
+
return path.resolve(this._cacheDir);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Returns an array with all the keys in the cache
|
|
226
|
+
* @method keys
|
|
227
|
+
* @returns {Array}
|
|
228
|
+
*/
|
|
229
|
+
keys() {
|
|
230
|
+
return [...this._cache.keys];
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* (Legacy) set key method. This method will be deprecated in the future
|
|
234
|
+
* @method setKey
|
|
235
|
+
* @param key {string} the key to set
|
|
236
|
+
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
237
|
+
*/
|
|
238
|
+
setKey(key, value, ttl) {
|
|
239
|
+
this.set(key, value, ttl);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Sets a key to a given value
|
|
243
|
+
* @method set
|
|
244
|
+
* @param key {string} the key to set
|
|
245
|
+
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
|
246
|
+
* @param [ttl] {number} the time to live in milliseconds
|
|
247
|
+
*/
|
|
248
|
+
set(key, value, ttl) {
|
|
249
|
+
this._cache.set(key, value, ttl);
|
|
250
|
+
this._changesSinceLastSave = true;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* (Legacy) Remove a given key from the cache. This method will be deprecated in the future
|
|
254
|
+
* @method removeKey
|
|
255
|
+
* @param key {String} the key to remove from the object
|
|
256
|
+
*/
|
|
257
|
+
removeKey(key) {
|
|
258
|
+
this.delete(key);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Remove a given key from the cache
|
|
262
|
+
* @method delete
|
|
263
|
+
* @param key {String} the key to remove from the object
|
|
264
|
+
*/
|
|
265
|
+
delete(key) {
|
|
266
|
+
this._cache.delete(key);
|
|
267
|
+
this._changesSinceLastSave = true;
|
|
268
|
+
this.emit("delete", key);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* (Legacy) Return the value of the provided key. This method will be deprecated in the future
|
|
272
|
+
* @method getKey<T>
|
|
273
|
+
* @param key {String} the name of the key to retrieve
|
|
274
|
+
* @returns {*} at T the value from the key
|
|
275
|
+
*/
|
|
276
|
+
getKey(key) {
|
|
277
|
+
return this.get(key);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Return the value of the provided key
|
|
281
|
+
* @method get<T>
|
|
282
|
+
* @param key {String} the name of the key to retrieve
|
|
283
|
+
* @returns {*} at T the value from the key
|
|
284
|
+
*/
|
|
285
|
+
get(key) {
|
|
286
|
+
return this._cache.get(key);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Clear the cache and save the state to disk
|
|
290
|
+
* @method clear
|
|
291
|
+
*/
|
|
292
|
+
clear() {
|
|
293
|
+
try {
|
|
294
|
+
this._cache.clear();
|
|
295
|
+
this._changesSinceLastSave = true;
|
|
296
|
+
this.save();
|
|
297
|
+
this.emit("clear");
|
|
298
|
+
} catch (error) {
|
|
299
|
+
/* v8 ignore next -- @preserve */
|
|
300
|
+
this.emit("error", error);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Save the state of the cache identified by the docId to disk
|
|
305
|
+
* as a JSON structure
|
|
306
|
+
* @method save
|
|
307
|
+
*/
|
|
308
|
+
save(force = false) {
|
|
309
|
+
try {
|
|
310
|
+
/* v8 ignore next -- @preserve */
|
|
311
|
+
if (this._changesSinceLastSave || force) {
|
|
312
|
+
const filePath = this.cacheFilePath;
|
|
313
|
+
const items = [...this._cache.items];
|
|
314
|
+
const data = this._stringify(items);
|
|
315
|
+
if (!fs.existsSync(this._cacheDir)) fs.mkdirSync(this._cacheDir, { recursive: true });
|
|
316
|
+
fs.writeFileSync(filePath, data);
|
|
317
|
+
this._changesSinceLastSave = false;
|
|
318
|
+
this.emit("save");
|
|
319
|
+
}
|
|
320
|
+
} catch (error) {
|
|
321
|
+
/* v8 ignore next -- @preserve */
|
|
322
|
+
this.emit("error", error);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Remove the file where the cache is persisted
|
|
327
|
+
* @method removeCacheFile
|
|
328
|
+
* @return {Boolean} true or false if the file was successfully deleted
|
|
329
|
+
*/
|
|
330
|
+
removeCacheFile() {
|
|
331
|
+
try {
|
|
332
|
+
if (fs.existsSync(this.cacheFilePath)) {
|
|
333
|
+
fs.rmSync(this.cacheFilePath);
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
/* v8 ignore next -- @preserve */
|
|
338
|
+
this.emit("error", error);
|
|
339
|
+
}
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Destroy the cache. This will remove the directory, file, and memory cache
|
|
344
|
+
* @method destroy
|
|
345
|
+
* @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed
|
|
346
|
+
* @return {undefined}
|
|
347
|
+
*/
|
|
348
|
+
destroy(includeCacheDirectory = false) {
|
|
349
|
+
try {
|
|
350
|
+
this._cache.clear();
|
|
351
|
+
this.stopAutoPersist();
|
|
352
|
+
if (includeCacheDirectory) fs.rmSync(this.cacheDirPath, {
|
|
353
|
+
recursive: true,
|
|
354
|
+
force: true
|
|
355
|
+
});
|
|
356
|
+
else fs.rmSync(this.cacheFilePath, {
|
|
357
|
+
recursive: true,
|
|
358
|
+
force: true
|
|
359
|
+
});
|
|
360
|
+
this._changesSinceLastSave = false;
|
|
361
|
+
this.emit("destroy");
|
|
362
|
+
} catch (error) {
|
|
363
|
+
/* v8 ignore next -- @preserve */
|
|
364
|
+
this.emit("error", error);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Start the auto persist interval
|
|
369
|
+
* @method startAutoPersist
|
|
370
|
+
*/
|
|
371
|
+
startAutoPersist() {
|
|
372
|
+
/* v8 ignore next -- @preserve */
|
|
373
|
+
if (this._persistInterval > 0) {
|
|
374
|
+
if (this._persistTimer) {
|
|
375
|
+
clearInterval(this._persistTimer);
|
|
376
|
+
this._persistTimer = void 0;
|
|
377
|
+
}
|
|
378
|
+
this._persistTimer = setInterval(() => {
|
|
379
|
+
this.save();
|
|
380
|
+
}, this._persistInterval);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Stop the auto persist interval
|
|
385
|
+
* @method stopAutoPersist
|
|
386
|
+
*/
|
|
387
|
+
stopAutoPersist() {
|
|
388
|
+
if (this._persistTimer) {
|
|
389
|
+
clearInterval(this._persistTimer);
|
|
390
|
+
this._persistTimer = void 0;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
var FlatCacheDefault = class {
|
|
395
|
+
static create = create;
|
|
396
|
+
static createFromFile = createFromFile;
|
|
397
|
+
static clearCacheById = clearCacheById;
|
|
398
|
+
static clearAll = clearAll;
|
|
399
|
+
};
|
|
400
|
+
/**
|
|
401
|
+
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
|
402
|
+
* cache storage.
|
|
403
|
+
*
|
|
404
|
+
* @method create
|
|
405
|
+
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
|
406
|
+
* @param cacheDirectory {String} directory for the cache entry
|
|
407
|
+
* @param options {FlatCacheOptions} options for the cache
|
|
408
|
+
* @returns {cache} cache instance
|
|
409
|
+
*/
|
|
410
|
+
function create(options) {
|
|
411
|
+
const cache = new FlatCache(options);
|
|
412
|
+
cache.load();
|
|
413
|
+
return cache;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Load a cache from the provided file
|
|
417
|
+
* @method createFromFile
|
|
418
|
+
* @param {String} filePath the path to the file containing the info for the cache
|
|
419
|
+
* @param options {FlatCacheOptions} options for the cache
|
|
420
|
+
* @returns {cache} cache instance
|
|
421
|
+
*/
|
|
422
|
+
function createFromFile(filePath, options) {
|
|
423
|
+
const cache = new FlatCache(options);
|
|
424
|
+
cache.loadFile(filePath);
|
|
425
|
+
return cache;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Clear the cache identified by the given Id. This will only remove the cache from disk.
|
|
429
|
+
* @method clearCacheById
|
|
430
|
+
* @param cacheId {String} the id of the cache
|
|
431
|
+
* @param cacheDirectory {String} directory for the cache entry
|
|
432
|
+
*/
|
|
433
|
+
function clearCacheById(cacheId, cacheDirectory) {
|
|
434
|
+
new FlatCache({
|
|
435
|
+
cacheId,
|
|
436
|
+
cacheDir: cacheDirectory
|
|
437
|
+
}).destroy();
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Clear the cache directory
|
|
441
|
+
* @method clearAll
|
|
442
|
+
* @param cacheDir {String} directory for the cache entry
|
|
443
|
+
*/
|
|
444
|
+
function clearAll(cacheDirectory) {
|
|
445
|
+
fs.rmSync(cacheDirectory ?? ".cache", {
|
|
446
|
+
recursive: true,
|
|
447
|
+
force: true
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
//#endregion
|
|
451
|
+
export { FlatCache, FlatCacheEvents, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default };
|