feat: Add CachedResourceSet

Uses a WeakMap on the identifier to cache resource existence.
This commit is contained in:
Joachim Van Herwegen
2022-02-25 10:30:26 +01:00
parent 4404fa07d9
commit 0e4d012086
4 changed files with 70 additions and 0 deletions

View File

@@ -349,6 +349,7 @@ export * from './storage/validators/QuotaValidator';
export * from './storage/AtomicResourceStore';
export * from './storage/BaseResourceStore';
export * from './storage/BasicConditions';
export * from './storage/CachedResourceSet';
export * from './storage/Conditions';
export * from './storage/DataAccessorBasedStore';
export * from './storage/IndexRepresentationStore';

View File

@@ -0,0 +1,24 @@
import type { ResourceIdentifier } from '../http/representation/ResourceIdentifier';
import type { ResourceSet } from './ResourceSet';
/**
* Caches resource existence in a `WeakMap` tied to the `ResourceIdentifier` object.
*/
export class CachedResourceSet implements ResourceSet {
private readonly source: ResourceSet;
private readonly cache: WeakMap<ResourceIdentifier, boolean>;
public constructor(source: ResourceSet) {
this.source = source;
this.cache = new WeakMap();
}
public async hasResource(identifier: ResourceIdentifier): Promise<boolean> {
if (this.cache.has(identifier)) {
return this.cache.get(identifier)!;
}
const result = await this.source.hasResource(identifier);
this.cache.set(identifier, result);
return result;
}
}