```typescript
export class LruCache<K, V> {
    private readonly capacity: number;
    private map = new Map<K, { value: V; timestamp: number }>();
    private time = 0;

    constructor(capacity: number) {
        if (typeof capacity !== 'number' || isNaN(capacity) || capacity <= 0) {
            throw new Error('Capacity must be a positive integer.');
        }
        this.capacity = capacity;
    }

    get(key: K): V | undefined {
        const now = ++this.time;
        if (this.map.has(key)) {
            this.map.set(key, { value: this.map.get(key)!.value, timestamp: now });
            return this.map.get(key)!.value;
        }
        return undefined;
    }

    set(key: K, value: V): void {
        const now = ++this.time;
        if (this.map.has(key)) {
            this.map.set(key, { value, timestamp: now });
        } else {
            if (this.map.size >= this.capacity) {
                const lru = Array.from(this.map.entries()).sort((a, b) => a[1].timestamp - b[1].timestamp)[0];
                this.map.delete(lru[0]);
            }
            this.map.set(key, { value, timestamp: now });
        }
    }

    get size(): number {
        return this.map.size;
    }
}
```