silverwolf-x/project / packages /next /src /lib /with-promise-cache.ts
silverwolf-x's picture
download
raw
799 Bytes
import type { LRUCache } from '../server/lib/lru-cache'
export function withPromiseCache<K, V>(
cache: LRUCache<Promise<V>>,
fn: (value: K) => Promise<V>
): (value: K) => Promise<V>
export function withPromiseCache<T extends any[], K, V>(
cache: LRUCache<Promise<V>>,
fn: (...values: T) => Promise<V>,
getKey: (...values: T) => K
): (...values: T) => Promise<V>
export function withPromiseCache<T extends any[], K, V>(
cache: LRUCache<Promise<V>>,
fn: (...values: T) => Promise<V>,
getKey?: (...values: T) => K
): (...values: T) => Promise<V> {
return (...values: T) => {
const key = getKey ? getKey(...values) : values[0]
let p = cache.get(key)
if (!p) {
p = fn(...values)
p.catch(() => cache.remove(key))
cache.set(key, p)
}
return p
}
}

Xet Storage Details

Size:
799 Bytes
·
Xet hash:
4457ae2962a35e42e3e975f37a643a56af765f48494cf550594c414c8b27e51d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.