Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+73
-7
@@ -26,10 +26,13 @@ export interface TwoTierCacheOptions<T> {
|
||||
/** TTL applied to L2 (and used as L1 max-age when the cache honours TTL). */
|
||||
ttlSeconds: number;
|
||||
/**
|
||||
* Optional L1 implementation. Defaults to an internal `Map` with no
|
||||
* eviction; pass an `LRUCache` if you need bounded memory.
|
||||
* Optional L1 implementation. Defaults to an internal TTL-aware LRU cache.
|
||||
*/
|
||||
l1Cache?: TwoTierL1Cache<T>;
|
||||
/** Maximum entries in the default L1 cache. Defaults to 10,000. */
|
||||
l1MaxEntries?: number;
|
||||
/** Override the L1 clock for deterministic tests. Defaults to `Date.now`. */
|
||||
l1Now?: () => number;
|
||||
/** Optional JSON serializer override for the L2 tier. */
|
||||
serializer?: CacheSerializer<T>;
|
||||
/**
|
||||
@@ -100,23 +103,80 @@ export interface TwoTierCache<T> {
|
||||
readonly namespace: string;
|
||||
}
|
||||
|
||||
class MapL1Cache<T> implements TwoTierL1Cache<T> {
|
||||
private readonly map = new Map<string, T>();
|
||||
interface L1Entry<T> {
|
||||
value: T;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
class BoundedTtlL1Cache<T> implements TwoTierL1Cache<T> {
|
||||
private readonly map = new Map<string, L1Entry<T>>();
|
||||
private expiryQueue: Array<{ key: string; expiresAt: number }> = [];
|
||||
private expiryQueueHead = 0;
|
||||
|
||||
constructor(
|
||||
private readonly maxEntries: number,
|
||||
private readonly ttlMs: number,
|
||||
private readonly clock: () => number,
|
||||
) {}
|
||||
|
||||
get(key: string): T | undefined {
|
||||
return this.map.get(key);
|
||||
const entry = this.map.get(key);
|
||||
if (!entry) return undefined;
|
||||
if (entry.expiresAt <= this.clock()) {
|
||||
this.map.delete(key);
|
||||
return undefined;
|
||||
}
|
||||
// Reinsert hits so Map insertion order acts as an O(1) LRU queue.
|
||||
this.map.delete(key);
|
||||
this.map.set(key, entry);
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
set(key: string, value: T): void {
|
||||
this.map.set(key, value);
|
||||
const now = this.clock();
|
||||
this.pruneExpired(now);
|
||||
this.map.delete(key);
|
||||
const expiresAt = now + this.ttlMs;
|
||||
this.map.set(key, { value, expiresAt });
|
||||
this.expiryQueue.push({ key, expiresAt });
|
||||
while (this.map.size > this.maxEntries) {
|
||||
const oldest = this.map.keys().next().value as string | undefined;
|
||||
if (oldest === undefined) break;
|
||||
this.map.delete(oldest);
|
||||
}
|
||||
}
|
||||
|
||||
delete(key: string): boolean {
|
||||
return this.map.delete(key);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.map.clear();
|
||||
this.expiryQueue = [];
|
||||
this.expiryQueueHead = 0;
|
||||
}
|
||||
|
||||
keys(): IterableIterator<string> {
|
||||
this.pruneExpired(this.clock());
|
||||
return this.map.keys();
|
||||
}
|
||||
|
||||
private pruneExpired(now: number): void {
|
||||
// Expiry order is independent from LRU order so cache hits cannot move an
|
||||
// expired entry behind a live one. Stale queue records from overwrites and
|
||||
// LRU evictions are ignored by comparing their timestamp with the map.
|
||||
while (this.expiryQueueHead < this.expiryQueue.length) {
|
||||
const candidate = this.expiryQueue[this.expiryQueueHead];
|
||||
if (candidate.expiresAt > now) break;
|
||||
this.expiryQueueHead += 1;
|
||||
const current = this.map.get(candidate.key);
|
||||
if (current?.expiresAt === candidate.expiresAt) this.map.delete(candidate.key);
|
||||
}
|
||||
if (this.expiryQueueHead >= 1_024 && this.expiryQueueHead * 2 >= this.expiryQueue.length) {
|
||||
this.expiryQueue = this.expiryQueue.slice(this.expiryQueueHead);
|
||||
this.expiryQueueHead = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,7 +203,13 @@ export function createTwoTierCache<T>(options: TwoTierCacheOptions<T>): TwoTierC
|
||||
if (options.ttlSeconds <= 0) {
|
||||
throw new Error("createTwoTierCache: ttlSeconds must be > 0");
|
||||
}
|
||||
const l1 = options.l1Cache ?? new MapL1Cache<T>();
|
||||
const l1MaxEntries = options.l1MaxEntries ?? 10_000;
|
||||
if (!Number.isSafeInteger(l1MaxEntries) || l1MaxEntries <= 0) {
|
||||
throw new Error("createTwoTierCache: l1MaxEntries must be a positive safe integer");
|
||||
}
|
||||
const l1 =
|
||||
options.l1Cache ??
|
||||
new BoundedTtlL1Cache<T>(l1MaxEntries, options.ttlSeconds * 1_000, options.l1Now ?? (() => Date.now()));
|
||||
const l2 = createCacheStore<T>({
|
||||
store: options.store,
|
||||
prefix: options.namespace,
|
||||
|
||||
Reference in new issue
Block a user