@seip/bluebird-js 0.0.0-stage → 0.0.1
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/.env_example +54 -0
- package/AGENTS.md +403 -0
- package/LICENSE +21 -0
- package/README.md +284 -2
- package/app/routes/api.js +50 -0
- package/app/routes/web.js +53 -0
- package/app/views/about.html +161 -0
- package/app/views/dashboard.html +140 -0
- package/app/views/index.html +150 -0
- package/app/views/login.html +144 -0
- package/core/app.d.ts +18 -0
- package/core/app.js +644 -0
- package/core/auth.d.ts +13 -0
- package/core/auth.js +326 -0
- package/core/cache.d.ts +17 -0
- package/core/cache.js +475 -0
- package/core/cli/doctor.js +287 -0
- package/core/cli/ecosystem.js +162 -0
- package/core/cli/init.js +779 -0
- package/core/cli/migrate.js +541 -0
- package/core/cli/model.js +66 -0
- package/core/cli/nginx.js +199 -0
- package/core/cli/route.js +121 -0
- package/core/cli/swagger.js +45 -0
- package/core/cli/view.js +77 -0
- package/core/config.d.ts +3 -0
- package/core/config.js +111 -0
- package/core/csrf.d.ts +3 -0
- package/core/csrf.js +114 -0
- package/core/database.d.ts +15 -0
- package/core/database.js +636 -0
- package/core/debug.d.ts +3 -0
- package/core/debug.js +250 -0
- package/core/hash.d.ts +3 -0
- package/core/hash.js +194 -0
- package/core/index.d.ts +1071 -0
- package/core/logger.d.ts +3 -0
- package/core/logger.js +124 -0
- package/core/middleware.d.ts +3 -0
- package/core/middleware.js +28 -0
- package/core/model.d.ts +19 -0
- package/core/model.js +600 -0
- package/core/queue.d.ts +13 -0
- package/core/queue.js +172 -0
- package/core/ratelimit.d.ts +3 -0
- package/core/ratelimit.js +145 -0
- package/core/redis.d.ts +21 -0
- package/core/redis.js +230 -0
- package/core/render.d.ts +3 -0
- package/core/render.js +676 -0
- package/core/router.d.ts +3 -0
- package/core/router.js +171 -0
- package/core/seo.d.ts +3 -0
- package/core/seo.js +106 -0
- package/core/swagger.d.ts +3 -0
- package/core/swagger.js +60 -0
- package/core/upload.d.ts +13 -0
- package/core/upload.js +102 -0
- package/core/util/colors.js +82 -0
- package/core/validate.d.ts +15 -0
- package/core/validate.js +364 -0
- package/core/vite.d.ts +3 -0
- package/core/vite.js +145 -0
- package/core/ws.d.ts +13 -0
- package/core/ws.js +245 -0
- package/index.js +18 -0
- package/jsconfig.json +15 -0
- package/package.json +84 -3
- package/production.md +273 -0
- package/public/css/bluebird.min.css +2 -0
- package/public/images/favicon.ico +0 -0
- package/public/js/bluebird-client.js +1663 -0
- package/public/js/bluebird.min.js +1073 -0
package/core/cache.js
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import Config from "./config.js";
|
|
5
|
+
import redisHandler, { getRedisClient } from "./redis.js";
|
|
6
|
+
|
|
7
|
+
const CACHE_MAX_KEYS = parseInt(process.env.CACHE_MAX_KEYS || "1000", 10);
|
|
8
|
+
const CACHE_PREFIX = "bluebird:cache:";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Resolves the writable filesystem cache directory.
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
function getFileCacheDir() {
|
|
15
|
+
return path.resolve(Config.userDataDir(), process.env.FILE_CACHE_DIR || "app/cache");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* LRU in-memory store backed by a Map.
|
|
20
|
+
* @type {Map<string, {type: string, data: any, expiry: number}>}
|
|
21
|
+
*/
|
|
22
|
+
const lruMap = new Map();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Single-flight in-flight promise registry to deduplicate concurrent lookups.
|
|
26
|
+
* @type {Map<string, Promise<any>>}
|
|
27
|
+
*/
|
|
28
|
+
const inflightPromises = new Map();
|
|
29
|
+
|
|
30
|
+
const rawCacheMode = (process.env.CACHE_MODE || "").toLowerCase().trim();
|
|
31
|
+
const DEFAULT_DRIVER = rawCacheMode === "redis" || rawCacheMode === "file" ? rawCacheMode : "memory";
|
|
32
|
+
const isNoneMode = rawCacheMode === "none" || rawCacheMode === "disabled" || rawCacheMode === "false";
|
|
33
|
+
|
|
34
|
+
let fileCacheInitialized = false;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Ensures the filesystem cache directory exists.
|
|
38
|
+
*/
|
|
39
|
+
function ensureFileCacheDir() {
|
|
40
|
+
if (fileCacheInitialized) return;
|
|
41
|
+
try {
|
|
42
|
+
const dir = getFileCacheDir();
|
|
43
|
+
if (!fs.existsSync(dir)) {
|
|
44
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
45
|
+
}
|
|
46
|
+
fileCacheInitialized = true;
|
|
47
|
+
} catch {
|
|
48
|
+
fileCacheInitialized = false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generates a filesystem safe file path for a cache key.
|
|
54
|
+
* @param {string} key
|
|
55
|
+
* @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
function getFileCachePath(key) {
|
|
58
|
+
const hash = crypto.createHash("sha256").update(key).digest("hex");
|
|
59
|
+
return path.join(getFileCacheDir(), `${hash}.json`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Reads a cached payload from the filesystem.
|
|
64
|
+
* @param {string} key
|
|
65
|
+
* @returns {{type: string, data: any, expiry: number}|null}
|
|
66
|
+
*/
|
|
67
|
+
function readFileCache(key) {
|
|
68
|
+
ensureFileCacheDir();
|
|
69
|
+
const filePath = getFileCachePath(key);
|
|
70
|
+
try {
|
|
71
|
+
if (!fs.existsSync(filePath)) return null;
|
|
72
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
73
|
+
const entry = JSON.parse(content);
|
|
74
|
+
if (entry.expiry && entry.expiry <= Date.now()) {
|
|
75
|
+
try {
|
|
76
|
+
fs.unlinkSync(filePath);
|
|
77
|
+
} catch {}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return entry;
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Writes a cache entry to the filesystem.
|
|
88
|
+
* @param {string} key
|
|
89
|
+
* @param {Object} entry
|
|
90
|
+
*/
|
|
91
|
+
function writeFileCache(key, entry) {
|
|
92
|
+
ensureFileCacheDir();
|
|
93
|
+
const filePath = getFileCachePath(key);
|
|
94
|
+
try {
|
|
95
|
+
fs.writeFileSync(filePath, JSON.stringify(entry), "utf-8");
|
|
96
|
+
} catch {}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Deletes a cached entry from the filesystem.
|
|
101
|
+
* @param {string} key
|
|
102
|
+
*/
|
|
103
|
+
function deleteFileCache(key) {
|
|
104
|
+
ensureFileCacheDir();
|
|
105
|
+
const filePath = getFileCachePath(key);
|
|
106
|
+
try {
|
|
107
|
+
if (fs.existsSync(filePath)) {
|
|
108
|
+
fs.unlinkSync(filePath);
|
|
109
|
+
}
|
|
110
|
+
} catch {}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Clears all cached files from the filesystem cache directory.
|
|
115
|
+
*/
|
|
116
|
+
function clearFileCache() {
|
|
117
|
+
ensureFileCacheDir();
|
|
118
|
+
try {
|
|
119
|
+
const dir = getFileCacheDir();
|
|
120
|
+
if (fs.existsSync(dir)) {
|
|
121
|
+
const files = fs.readdirSync(dir);
|
|
122
|
+
for (const file of files) {
|
|
123
|
+
if (file.endsWith(".json")) {
|
|
124
|
+
try {
|
|
125
|
+
fs.unlinkSync(path.join(dir, file));
|
|
126
|
+
} catch {}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} catch {}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Evicts the oldest entry from the LRU map when the cap is reached.
|
|
135
|
+
*/
|
|
136
|
+
function evictIfNeeded() {
|
|
137
|
+
if (lruMap.size >= CACHE_MAX_KEYS) {
|
|
138
|
+
const oldestKey = lruMap.keys().next().value;
|
|
139
|
+
if (oldestKey !== undefined) lruMap.delete(oldestKey);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
setInterval(() => {
|
|
144
|
+
if (lruMap.size === 0) return;
|
|
145
|
+
const now = Date.now();
|
|
146
|
+
for (const [key, entry] of lruMap) {
|
|
147
|
+
if (entry.expiry <= now) lruMap.delete(key);
|
|
148
|
+
}
|
|
149
|
+
}, 300000).unref();
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* High-performance Cache manager supporting in-memory LRU (default),
|
|
153
|
+
* filesystem persistence fallback, and optional Redis driver.
|
|
154
|
+
*/
|
|
155
|
+
class Cache {
|
|
156
|
+
/**
|
|
157
|
+
* Express middleware to cache route JSON and HTML responses.
|
|
158
|
+
* @param {number} [seconds=60] - Expiry time in seconds.
|
|
159
|
+
* @param {Object} [options={}] - Override options.
|
|
160
|
+
* @param {"memory"|"file"|"redis"} [options.driver="memory"] - Cache driver to use.
|
|
161
|
+
* @param {boolean} [options.allowAuthenticated=false] - Whether to allow caching on authenticated requests.
|
|
162
|
+
* @param {Function} [options.key] - Custom key generator function: (req) => string.
|
|
163
|
+
* @returns {Function} Express middleware.
|
|
164
|
+
*/
|
|
165
|
+
static middleware(seconds = 60, options = {}) {
|
|
166
|
+
return async (req, res, next) => {
|
|
167
|
+
if (isNoneMode || req.method !== "GET") return next();
|
|
168
|
+
|
|
169
|
+
const hasAuth = Boolean(
|
|
170
|
+
req.cookies?.auth ||
|
|
171
|
+
req.cookies?.token ||
|
|
172
|
+
req.headers?.authorization ||
|
|
173
|
+
req.user
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
if (hasAuth && !options.allowAuthenticated && !options.key) {
|
|
177
|
+
return next();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const driver = options.driver || DEFAULT_DRIVER;
|
|
181
|
+
const cacheKeyPath = typeof options.key === "function" ? options.key(req) : req.originalUrl;
|
|
182
|
+
const key = `${CACHE_PREFIX}route:${cacheKeyPath}`;
|
|
183
|
+
|
|
184
|
+
const cached = await Cache.get(key, { ...options, driver, raw: true });
|
|
185
|
+
if (cached) {
|
|
186
|
+
res.set("X-Blue-Bird-Cache", "HIT");
|
|
187
|
+
if (cached.type === "json") return res.json(cached.data);
|
|
188
|
+
res.type("text/html");
|
|
189
|
+
return res.send(cached.data);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const originalJson = res.json.bind(res);
|
|
193
|
+
const originalSend = res.send.bind(res);
|
|
194
|
+
let stored = false;
|
|
195
|
+
|
|
196
|
+
res.json = (body) => {
|
|
197
|
+
if (!stored && res.statusCode >= 200 && res.statusCode < 300) {
|
|
198
|
+
stored = true;
|
|
199
|
+
Cache.set(key, body, seconds, { ...options, driver, type: "json" });
|
|
200
|
+
}
|
|
201
|
+
res.set("X-Blue-Bird-Cache", "MISS");
|
|
202
|
+
return originalJson(body);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
res.send = (body) => {
|
|
206
|
+
if (!stored && typeof body === "string" && res.statusCode >= 200 && res.statusCode < 300) {
|
|
207
|
+
stored = true;
|
|
208
|
+
Cache.set(key, body, seconds, { ...options, driver, type: "html" });
|
|
209
|
+
}
|
|
210
|
+
res.set("X-Blue-Bird-Cache", "MISS");
|
|
211
|
+
return originalSend(body);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
res.set("X-Blue-Bird-Cache", "MISS");
|
|
215
|
+
next();
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Retrieves a cached value by key.
|
|
221
|
+
* @param {string} key - Cache key identifier.
|
|
222
|
+
* @param {Object} [options={}] - Options.
|
|
223
|
+
* @param {"memory"|"file"|"redis"} [options.driver="memory"] - Target driver.
|
|
224
|
+
* @param {boolean} [options.raw=false] - Return internal container with metadata.
|
|
225
|
+
* @returns {Promise<any|null>} Cached payload or null.
|
|
226
|
+
*/
|
|
227
|
+
static async get(key, options = {}) {
|
|
228
|
+
if (isNoneMode) return null;
|
|
229
|
+
key = String(key).trim();
|
|
230
|
+
if (!key) return null;
|
|
231
|
+
|
|
232
|
+
const namespacedKey = key.startsWith(CACHE_PREFIX) ? key : `${CACHE_PREFIX}${key}`;
|
|
233
|
+
const driver = options.driver || DEFAULT_DRIVER;
|
|
234
|
+
|
|
235
|
+
if (driver === "redis") {
|
|
236
|
+
const data = await redisHandler.get(namespacedKey);
|
|
237
|
+
if (data !== null) {
|
|
238
|
+
if (options.raw) return data;
|
|
239
|
+
return data && typeof data === "object" && "data" in data ? data.data : data;
|
|
240
|
+
}
|
|
241
|
+
if (options.fallbackDriver === "file") {
|
|
242
|
+
const fallbackEntry = readFileCache(namespacedKey);
|
|
243
|
+
if (fallbackEntry) {
|
|
244
|
+
if (options.raw) return fallbackEntry;
|
|
245
|
+
return fallbackEntry.data !== undefined ? fallbackEntry.data : fallbackEntry;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (driver === "file") {
|
|
251
|
+
const fileEntry = readFileCache(namespacedKey);
|
|
252
|
+
if (fileEntry) {
|
|
253
|
+
if (options.raw) return fileEntry;
|
|
254
|
+
return fileEntry.data !== undefined ? fileEntry.data : fileEntry;
|
|
255
|
+
}
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const entry = lruMap.get(namespacedKey) || lruMap.get(key);
|
|
260
|
+
if (entry) {
|
|
261
|
+
if (entry.expiry > Date.now()) {
|
|
262
|
+
const activeKey = lruMap.has(namespacedKey) ? namespacedKey : key;
|
|
263
|
+
lruMap.delete(activeKey);
|
|
264
|
+
lruMap.set(activeKey, entry);
|
|
265
|
+
if (options.raw) return entry;
|
|
266
|
+
return entry.data !== undefined ? entry.data : entry;
|
|
267
|
+
}
|
|
268
|
+
lruMap.delete(namespacedKey);
|
|
269
|
+
lruMap.delete(key);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (options.persistFile) {
|
|
273
|
+
const fallbackEntry = readFileCache(namespacedKey);
|
|
274
|
+
if (fallbackEntry) {
|
|
275
|
+
if (options.raw) return fallbackEntry;
|
|
276
|
+
return fallbackEntry.data !== undefined ? fallbackEntry.data : fallbackEntry;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Stores data in cache with an expiration TTL.
|
|
285
|
+
* @param {string} key - Cache key.
|
|
286
|
+
* @param {any} value - Value to cache.
|
|
287
|
+
* @param {number} [seconds=60] - Expiry TTL in seconds.
|
|
288
|
+
* @param {Object} [options={}] - Options.
|
|
289
|
+
* @param {"memory"|"file"|"redis"} [options.driver="memory"] - Target driver.
|
|
290
|
+
* @param {"memory"|"file"} [options.fallbackDriver="file"] - Fallback driver if primary fails.
|
|
291
|
+
* @returns {Promise<boolean>}
|
|
292
|
+
*/
|
|
293
|
+
static async set(key, value, seconds = 60, options = {}) {
|
|
294
|
+
if (isNoneMode) return true;
|
|
295
|
+
key = String(key).trim();
|
|
296
|
+
if (!key) return false;
|
|
297
|
+
|
|
298
|
+
const namespacedKey = key.startsWith(CACHE_PREFIX) ? key : `${CACHE_PREFIX}${key}`;
|
|
299
|
+
const driver = options.driver || DEFAULT_DRIVER;
|
|
300
|
+
const fallbackDriver = options.fallbackDriver || "memory";
|
|
301
|
+
|
|
302
|
+
const entryType = options.type || (typeof value === "string" ? "html" : "json");
|
|
303
|
+
const obj = {
|
|
304
|
+
type: entryType,
|
|
305
|
+
data: value,
|
|
306
|
+
expiry: Date.now() + seconds * 1000,
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
if (driver === "redis") {
|
|
310
|
+
const success = await redisHandler.set(namespacedKey, obj, seconds);
|
|
311
|
+
if (success) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
if (fallbackDriver === "file") {
|
|
315
|
+
writeFileCache(namespacedKey, obj);
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
evictIfNeeded();
|
|
319
|
+
lruMap.set(namespacedKey, obj);
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (driver === "file") {
|
|
324
|
+
writeFileCache(namespacedKey, obj);
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
evictIfNeeded();
|
|
329
|
+
lruMap.set(namespacedKey, obj);
|
|
330
|
+
|
|
331
|
+
if (options.persistFile) {
|
|
332
|
+
writeFileCache(namespacedKey, obj);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Checks whether a cache key exists and has not expired.
|
|
340
|
+
* @param {string} key
|
|
341
|
+
* @param {Object} [options={}]
|
|
342
|
+
* @returns {Promise<boolean>}
|
|
343
|
+
*/
|
|
344
|
+
static async has(key, options = {}) {
|
|
345
|
+
const val = await Cache.get(key, options);
|
|
346
|
+
return val !== null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Executes an async loader with single-flight deduplication.
|
|
351
|
+
* @param {string} key - Deduplication key identifier.
|
|
352
|
+
* @param {Function} loader - Async loader function.
|
|
353
|
+
* @returns {Promise<any>}
|
|
354
|
+
*/
|
|
355
|
+
static async deduplicate(key, loader) {
|
|
356
|
+
if (inflightPromises.has(key)) {
|
|
357
|
+
return inflightPromises.get(key);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const promise = Promise.resolve().then(() => loader()).finally(() => {
|
|
361
|
+
inflightPromises.delete(key);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
inflightPromises.set(key, promise);
|
|
365
|
+
return promise;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Deletes one or more keys from cache.
|
|
370
|
+
* @param {string|string[]} keys - Cache key or keys.
|
|
371
|
+
* @param {Object} [options={}] - Options.
|
|
372
|
+
* @param {"memory"|"file"|"redis"|"all"} [options.driver="all"] - Target driver.
|
|
373
|
+
* @returns {Promise<boolean>}
|
|
374
|
+
*/
|
|
375
|
+
static async delete(keys, options = {}) {
|
|
376
|
+
if (isNoneMode || !keys) return false;
|
|
377
|
+
const keyList = Array.isArray(keys) ? keys : [keys];
|
|
378
|
+
const driver = options.driver || "all";
|
|
379
|
+
|
|
380
|
+
for (const key of keyList) {
|
|
381
|
+
const namespacedKey = key.startsWith(CACHE_PREFIX) ? key : `${CACHE_PREFIX}${key}`;
|
|
382
|
+
|
|
383
|
+
if (driver === "all" || driver === "memory") {
|
|
384
|
+
lruMap.delete(namespacedKey);
|
|
385
|
+
lruMap.delete(key);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (driver === "all" || driver === "file") {
|
|
389
|
+
deleteFileCache(namespacedKey);
|
|
390
|
+
deleteFileCache(key);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (driver === "redis" || (driver === "all" && (DEFAULT_DRIVER === "redis" || redisHandler.isRedisConnected()))) {
|
|
394
|
+
await redisHandler.del([namespacedKey, key]);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Deletes all cache entries matching a specific key prefix across memory and Redis.
|
|
403
|
+
* @param {string} prefix - Prefix to match (e.g. "db:users:").
|
|
404
|
+
* @returns {Promise<boolean>}
|
|
405
|
+
*/
|
|
406
|
+
static async deletePrefix(prefix) {
|
|
407
|
+
if (isNoneMode || !prefix) return false;
|
|
408
|
+
const namespacedPrefix = prefix.startsWith(CACHE_PREFIX) ? prefix : `${CACHE_PREFIX}${prefix}`;
|
|
409
|
+
|
|
410
|
+
for (const key of lruMap.keys()) {
|
|
411
|
+
if (key.startsWith(namespacedPrefix) || key.startsWith(prefix)) {
|
|
412
|
+
lruMap.delete(key);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
if (DEFAULT_DRIVER === "redis" || redisHandler.isRedisConnected()) {
|
|
417
|
+
await redisHandler.clearPrefix(namespacedPrefix);
|
|
418
|
+
}
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Alias for delete().
|
|
424
|
+
* @param {string|string[]} keys
|
|
425
|
+
* @param {Object} [options={}]
|
|
426
|
+
* @returns {Promise<boolean>}
|
|
427
|
+
*/
|
|
428
|
+
static async del(keys, options = {}) {
|
|
429
|
+
return this.delete(keys, options);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Flushes cached entries across memory, filesystem, and Redis.
|
|
434
|
+
* @param {Object} [options={}]
|
|
435
|
+
* @param {"memory"|"file"|"redis"|"all"} [options.driver="all"]
|
|
436
|
+
* @returns {Promise<boolean>}
|
|
437
|
+
*/
|
|
438
|
+
static async clear(options = {}) {
|
|
439
|
+
const driver = options.driver || "all";
|
|
440
|
+
|
|
441
|
+
if (driver === "all" || driver === "memory") {
|
|
442
|
+
lruMap.clear();
|
|
443
|
+
inflightPromises.clear();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (driver === "all" || driver === "file") {
|
|
447
|
+
clearFileCache();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (driver === "redis" || (driver === "all" && (DEFAULT_DRIVER === "redis" || redisHandler.isRedisConnected()))) {
|
|
451
|
+
await redisHandler.clearPrefix(CACHE_PREFIX);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Returns the current in-memory entry count.
|
|
459
|
+
* @returns {number}
|
|
460
|
+
*/
|
|
461
|
+
static size() {
|
|
462
|
+
return lruMap.size;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Returns the active default cache mode.
|
|
467
|
+
* @returns {string}
|
|
468
|
+
*/
|
|
469
|
+
static getMode() {
|
|
470
|
+
return DEFAULT_DRIVER;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
export { Cache, getRedisClient };
|
|
475
|
+
export default Cache;
|