Skip to content

Commit 3b13b59

Browse files
committed
test(cache): reject empty cache keys
1 parent 6ede310 commit 3b13b59

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/cache.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,15 @@ describe('TTLCache', () => {
410410
cache.destroy();
411411
});
412412

413+
it('rejects an empty string cache key', () => {
414+
const cache = new TTLCache<string>();
415+
416+
expect(() => cache.set('', 'value', 60_000)).toThrow('Cache key cannot be empty');
417+
expect(cache.has('')).toBe(false);
418+
419+
cache.destroy();
420+
});
421+
413422
it('handles rapid get/set/delete cycles', () => {
414423
const cache = new TTLCache<number>();
415424
for (let i = 0; i < 100; i++) {

lib/cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class TTLCache<T> {
141141
}
142142

143143
set(key: string, value: T, ttlMs: number): void {
144+
if (key === '') throw new Error('Cache key cannot be empty');
144145
if (ttlMs <= 0) throw new RangeError(`ttlMs must be positive, got ${ttlMs}`);
145146

146147
const maxSize = this.maxSize;

0 commit comments

Comments
 (0)