separate get into get and getMaybe

This commit is contained in:
Hazelnoot 2025-11-12 01:00:16 -05:00
parent a2ddeb28c3
commit 79289f1a87
2 changed files with 9 additions and 7 deletions

View file

@ -399,13 +399,15 @@ export class QuantumKVCache<TIn, T extends Value<TIn> = Value<TIn>> implements I
/**
* Gets multiple values from the local memory cache; returning undefined for any missing keys.
* Returns cached data only - does not make any fetches.
* TODO don't return undefined, matching fetch
*/
@bindThis
public getMany(keys: Iterable<string>): [key: string, value: T | undefined][] {
const results: [key: string, value: T | undefined][] = [];
public getMany(keys: Iterable<string>): [key: string, value: T][] {
const results: [key: string, value: T][] = [];
for (const key of keys) {
results.push([key, this.getMaybe(key)]);
const value = this.getMaybe(key);
if (value !== undefined) {
results.push([key, value]);
}
}
return results;
}

View file

@ -308,7 +308,7 @@ describe(QuantumKVCache, () => {
expect(result).toEqual([]);
});
it('should return the value for all keys', () => {
it('should include the value of all found keys', () => {
const cache = makeCache();
cache.add('foo', 'bar');
cache.add('alpha', 'omega');
@ -318,13 +318,13 @@ describe(QuantumKVCache, () => {
expect(result).toEqual([['foo', 'bar'], ['alpha', 'omega']]);
});
it('should return undefined for missing keys', () => {
it('should exclude all missing keys', () => {
const cache = makeCache();
cache.add('foo', 'bar');
const result = cache.getMany(['foo', 'alpha']);
expect(result).toEqual([['foo', 'bar'], ['alpha', undefined]]);
expect(result).toEqual([['foo', 'bar']]);
});
});