Create permanent cache helper function
Public
The user wants me to add a helper function to the cached-value.ts file that creates a cache that lasts forever (maximum TTL). This helper should return a function that wraps around the .get() method.
Let me first read the current file to understand the structure.
@@ -130,4 +130,18 @@
throw errorObj
}
}
}
/**
* Creates a function that caches its result forever (or until process restart).
* Useful for expensive computations that should only run once.
*/
export function cachedForever<V>(compute: () => Promise<V>): () => Promise<V> {
const cache = new GlobalCachedValue<V, never>({
compute,
softTTL: Number.MAX_SAFE_INTEGER,
hardTTL: Number.MAX_SAFE_INTEGER,
changes: () => undefined,
})
return () => cache.get()
}
Added cachedForever() helper that returns a function wrapping .get() with maximum TTL values, effectively caching forever.
The user wants me to include the same warning that's in the GlobalCachedValue class docstring about when not to use this. Let me look at that warning:
/**
* A cached value with TTL-based recomputation and event emission.
*
* This class maintains internal state (Subject) that persists for the lifetime
* of the instance. It should only be used for long-lived, global caches. Do NOT use
* this for temporary or short-lived cached values as the Subject instance will be
* leaked.
*/
I should add similar warning to the cachedForever function's docstring. Since this creates a GlobalCachedValue internally, it has the same concerns.
@@ -134,8 +134,12 @@
/**
* Creates a function that caches its result forever (or until process restart).
* Useful for expensive computations that should only run once.
*
* This maintains internal state that persists for the lifetime of the process.
* It should only be used for long-lived, global caches. Do NOT use this for
* temporary or short-lived cached values as the cache instance will be leaked.
*/
export function cachedForever<V>(compute: () => Promise<V>): () => Promise<V> {
const cache = new GlobalCachedValue<V, never>({
compute,