mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 12:36:57 +00:00
implement QuantumKVCache.add and QuantumKVCache.addMany
This commit is contained in:
parent
bf1156426e
commit
b7624666d6
2 changed files with 84 additions and 0 deletions
|
@ -565,6 +565,28 @@ export class QuantumKVCache<T> implements Iterable<[key: string, value: T]> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a value to the local memory cache without notifying other process.
|
||||||
|
* Neither a Redis event nor onSet callback will be fired, as the value has not actually changed.
|
||||||
|
* This should only be used when the value is known to be current, like after fetching from the database.
|
||||||
|
*/
|
||||||
|
@bindThis
|
||||||
|
public add(key: string, value: T): void {
|
||||||
|
this.memoryCache.set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds multiple values to the local memory cache without notifying other process.
|
||||||
|
* Neither a Redis event nor onSet callback will be fired, as the value has not actually changed.
|
||||||
|
* This should only be used when the value is known to be current, like after fetching from the database.
|
||||||
|
*/
|
||||||
|
@bindThis
|
||||||
|
public addMany(items: Iterable<[key: string, value: T]>): void {
|
||||||
|
for (const [key, value] of items) {
|
||||||
|
this.memoryCache.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a value from the local memory cache, or returns undefined if not found.
|
* Gets a value from the local memory cache, or returns undefined if not found.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -417,6 +417,68 @@ describe(QuantumKVCache, () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('add', () => {
|
||||||
|
it('should add the item', () => {
|
||||||
|
const cache = makeCache();
|
||||||
|
cache.add('foo', 'bar');
|
||||||
|
expect(cache.has('foo')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not emit event', () => {
|
||||||
|
const cache = makeCache({
|
||||||
|
name: 'fake',
|
||||||
|
});
|
||||||
|
|
||||||
|
cache.add('foo', 'bar');
|
||||||
|
|
||||||
|
expect(fakeInternalEventService._calls.filter(c => c[0] === 'emit')).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not call onSet', () => {
|
||||||
|
const fakeOnSet = jest.fn(() => Promise.resolve());
|
||||||
|
const cache = makeCache({
|
||||||
|
onSet: fakeOnSet,
|
||||||
|
});
|
||||||
|
|
||||||
|
cache.add('foo', 'bar');
|
||||||
|
|
||||||
|
expect(fakeOnSet).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('addMany', () => {
|
||||||
|
it('should add all items', () => {
|
||||||
|
const cache = makeCache();
|
||||||
|
|
||||||
|
cache.addMany([['foo', 'bar'], ['alpha', 'omega']]);
|
||||||
|
|
||||||
|
expect(cache.has('foo')).toBe(true);
|
||||||
|
expect(cache.has('alpha')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should not emit event', () => {
|
||||||
|
const cache = makeCache({
|
||||||
|
name: 'fake',
|
||||||
|
});
|
||||||
|
|
||||||
|
cache.addMany([['foo', 'bar'], ['alpha', 'omega']]);
|
||||||
|
|
||||||
|
expect(fakeInternalEventService._calls.filter(c => c[0] === 'emit')).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not call onSet', () => {
|
||||||
|
const fakeOnSet = jest.fn(() => Promise.resolve());
|
||||||
|
const cache = makeCache({
|
||||||
|
onSet: fakeOnSet,
|
||||||
|
});
|
||||||
|
|
||||||
|
cache.addMany([['foo', 'bar'], ['alpha', 'omega']]);
|
||||||
|
|
||||||
|
expect(fakeOnSet).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('has', () => {
|
describe('has', () => {
|
||||||
it('should return false when empty', () => {
|
it('should return false when empty', () => {
|
||||||
const cache = makeCache();
|
const cache = makeCache();
|
||||||
|
|
Loading…
Add table
Reference in a new issue