```typescript
export class LruCache<K, V> {
    constructor(capacity: number) {
        if (capacity <= 0 || !Number.isInteger(capacity)) {
            throw new Error("Capacity must be a positive integer");
        }
        this.capacity = capacity;
        this.cache = new Map<K, V>();
        this.keys = [];
    }

    get(key: K): V | undefined {
        const value = this.cache.get(key);
        if (value !== undefined) {
            this.moveToRecent(key);
        }
        return value;
    }

    set(key: K, value: V): void {
        if (this.cache.has(key)) {
            this.cache.set(key, value);
            this.moveToRecent(key);
        } else {
            if (this.keys.length === this.capacity) {
                const oldestKey = this.keys.shift();
                if (oldestKey !== undefined) {
                    this.cache.delete(oldestKey);
                }
            }
            this.cache.set(key, value);
            this.keys.push(key);
        }
    }

    get size(): number {
        return this.keys.length;
    }

    private moveToRecent(key: K): void {
        const index = this.keys.indexOf(key);
        if (index !== -1) {
            this.keys.splice(index, 1);
            this.keys.push(key);
        }
    }
}
```