```typescript
export class LruCache<K, V> {
    private readonly capacity: number;
    private cache: Map<K, { value: V; timestamp: number }>;
    private time: number;

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

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

    set(key: K, value: V): void {
        if (this.cache.size >= this.capacity) {
            const oldestKey = Array.from(this.cache.keys()).reduce((oldest, current) => {
                const oldEntry = this.cache.get(oldest)!;
                const currEntry = this.cache.get(current)!;
                return oldEntry.timestamp < currEntry.timestamp ? oldest : current;
            });
            this.cache.delete(oldestKey);
        }
        this.cache.set(key, { value, timestamp: ++this.time });
    }

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