Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/lib/utils/cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it, expect } from 'vitest'
import { SimpleCache } from './cache'

describe('SimpleCache', () => {
it('basic set/get returns value', () => {
const cache = new SimpleCache<string, number>()
cache.set('foo', 123)
expect(cache.get('foo')).toBe(123)
})

it('expired entry returns undefined', async () => {
const cache = new SimpleCache<string, string>()
cache.set('bar', 'baz', 10)
await new Promise(r => setTimeout(r, 20))
expect(cache.get('bar')).toBeUndefined()
})

it('has() returns true before expiry, false after', async () => {
const cache = new SimpleCache<number, number>()
cache.set(1, 999, 30)
expect(cache.has(1)).toBe(true)
await new Promise(r => setTimeout(r, 35))
expect(cache.has(1)).toBe(false)
})

it('overwriting existing key updates value and TTL', async () => {
const cache = new SimpleCache<string, string>()
cache.set('dup', 'first', 50)
cache.set('dup', 'second', 10)
expect(cache.get('dup')).toBe('second')
await new Promise(r => setTimeout(r, 15))
expect(cache.get('dup')).toBeUndefined()
})

it('cache entry without TTL never expires', async () => {
const cache = new SimpleCache<string, string>()
cache.set('persist', 'value')
await new Promise(r => setTimeout(r, 30))
expect(cache.get('persist')).toBe('value')
expect(cache.has('persist')).toBe(true)
})

it('returns undefined for missing key', () => {
const cache = new SimpleCache<string, string>()
expect(cache.get('missing')).toBeUndefined()
expect(cache.has('missing')).toBe(false)
})

it('accepts empty string and zero as keys', () => {
const cache = new SimpleCache<string | number, string>()
cache.set('', 'empty')
cache.set(0, 'zero')
expect(cache.get('')).toBe('empty')
expect(cache.get(0)).toBe('zero')
expect(cache.has('')).toBe(true)
expect(cache.has(0)).toBe(true)
})

it('negative TTL disables expiration (acts as no TTL)', async () => {
const cache = new SimpleCache<string, string>()
cache.set('neg', 'negval', -100)
await new Promise(r => setTimeout(r, 10))
expect(cache.get('neg')).toBe('negval')
expect(cache.has('neg')).toBe(true)
})
})
35 changes: 35 additions & 0 deletions src/lib/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export class SimpleCache<K, V> {
private store: Map<K, { value: V; expiresAt?: number }>

constructor() {
this.store = new Map();
}

set(key: K, value: V, ttlMs?: number): void {
let expiresAt: number | undefined = undefined;
if (typeof ttlMs === 'number' && ttlMs > 0) {
expiresAt = Date.now() + ttlMs;
}
this.store.set(key, { value, expiresAt });
}

get(key: K): V | undefined {
const entry = this.store.get(key);
if (!entry) return undefined;
if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
this.store.delete(key);
return undefined;
}
return entry.value;
}

has(key: K): boolean {
const entry = this.store.get(key);
if (!entry) return false;
if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
this.store.delete(key);
return false;
}
return true;
}
}