```typescript
export class LruCache<K, V> {
    private capacity: number;
    private cache: Map<K, V>;
    private order: K[];

    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.order = [];
    }

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

    set(key: K, value: V): void {
        if (this.cache.has(key)) {
            this.cache.set(key, value);
        } else {
            if (this.cache.size >= this.capacity) {
                const lruKey = this.order.shift();
                if (lruKey !== undefined) {
                    this.cache.delete(lruKey);
                }
            }
            this.cache.set(key, value);
        }
        this.updateOrder(key);
    }

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

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