@oxy.so/core 1.1.1 → 1.1.2
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/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/utils/cache.js +28 -19
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/utils/cache.js +28 -19
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/utils/cache.d.ts +1 -4
- package/package.json +1 -1
- package/src/utils/__tests__/cacheLifecycle.test.ts +65 -0
- package/src/utils/cache.ts +22 -20
package/dist/cjs/utils/cache.js
CHANGED
|
@@ -85,6 +85,8 @@ class TTLCache {
|
|
|
85
85
|
const effectiveTTL = ttl === undefined ? this.defaultTTL : ttl;
|
|
86
86
|
const expiresAt = now + effectiveTTL;
|
|
87
87
|
this.cache.set(key, { data, timestamp: now, expiresAt });
|
|
88
|
+
if (!cleanupInterval && activeCaches.has(this))
|
|
89
|
+
updateCleanupInterval();
|
|
88
90
|
}
|
|
89
91
|
/**
|
|
90
92
|
* Delete a specific cache entry
|
|
@@ -92,7 +94,10 @@ class TTLCache {
|
|
|
92
94
|
* @returns true if entry was deleted, false if not found
|
|
93
95
|
*/
|
|
94
96
|
delete(key) {
|
|
95
|
-
|
|
97
|
+
const deleted = this.cache.delete(key);
|
|
98
|
+
if (activeCaches.has(this))
|
|
99
|
+
updateCleanupInterval();
|
|
100
|
+
return deleted;
|
|
96
101
|
}
|
|
97
102
|
/**
|
|
98
103
|
* Clear all cache entries
|
|
@@ -101,6 +106,8 @@ class TTLCache {
|
|
|
101
106
|
this.cache.clear();
|
|
102
107
|
this.hits = 0;
|
|
103
108
|
this.misses = 0;
|
|
109
|
+
if (activeCaches.has(this))
|
|
110
|
+
updateCleanupInterval();
|
|
104
111
|
}
|
|
105
112
|
/**
|
|
106
113
|
* Check if a key exists and is not expired
|
|
@@ -199,28 +206,30 @@ const activeCaches = new Set();
|
|
|
199
206
|
*/
|
|
200
207
|
function registerCacheForCleanup(cache) {
|
|
201
208
|
activeCaches.add(cache);
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
cleanupInterval
|
|
209
|
+
updateCleanupInterval();
|
|
210
|
+
}
|
|
211
|
+
/** Empty registered caches need no timer; importing a client must stay inert. */
|
|
212
|
+
function updateCleanupInterval() {
|
|
213
|
+
const hasEntries = [...activeCaches].some(cache => cache.size() > 0);
|
|
214
|
+
if (!hasEntries) {
|
|
215
|
+
if (cleanupInterval)
|
|
216
|
+
clearInterval(cleanupInterval);
|
|
217
|
+
cleanupInterval = null;
|
|
218
|
+
return;
|
|
211
219
|
}
|
|
220
|
+
if (cleanupInterval)
|
|
221
|
+
return;
|
|
222
|
+
cleanupInterval = setInterval(() => {
|
|
223
|
+
for (const cache of activeCaches)
|
|
224
|
+
cache.cleanup();
|
|
225
|
+
updateCleanupInterval();
|
|
226
|
+
}, 60000);
|
|
227
|
+
cleanupInterval.unref?.();
|
|
212
228
|
}
|
|
213
|
-
/**
|
|
214
|
-
* Unregister a cache from automatic cleanup
|
|
215
|
-
* @param cache Cache instance to unregister
|
|
216
|
-
*/
|
|
229
|
+
/** Unregister a cache without changing its entries or TTL behavior. */
|
|
217
230
|
function unregisterCacheFromCleanup(cache) {
|
|
218
231
|
activeCaches.delete(cache);
|
|
219
|
-
|
|
220
|
-
if (activeCaches.size === 0 && cleanupInterval) {
|
|
221
|
-
clearInterval(cleanupInterval);
|
|
222
|
-
cleanupInterval = null;
|
|
223
|
-
}
|
|
232
|
+
updateCleanupInterval();
|
|
224
233
|
}
|
|
225
234
|
/**
|
|
226
235
|
* Stop all cleanup intervals (useful for testing)
|