@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.
@@ -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
- return this.cache.delete(key);
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
- // Start cleanup interval if not already running
203
- if (!cleanupInterval) {
204
- cleanupInterval = setInterval(() => {
205
- for (const cache of activeCaches) {
206
- cache.cleanup();
207
- }
208
- }, 60000); // Every minute
209
- // Never block process/worker exit on a housekeeping timer.
210
- cleanupInterval.unref?.();
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
- // Stop cleanup interval if no caches are registered
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)