```typescript


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

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

    get(key: K): V | undefined {
        if (!this.cache.has(key)) return undefined;

        // Move the accessed key to the end of the order list
        const index = this.order.get(key) as number;
        this.order.delete(key);
        this.order.set(key, this.capacity - 1);

        return this.cache.get(key)!; // ! is safe here because we checked the cache has it
    }

    set(key: K, value: V): void {
        if (!this.cache.has(key)) {
            if (this.capacity === this.cache.size) {
                const lruKey = Array.from(this.order.keys()).shift()!;
                this.cache.delete(lruKey);
                this.order.delete(lruKey);
            }
        } else {
            // Update the existing key and move it to the end of the order list
            this.order.delete(key);
        }

        this.cache.set(key, value);
        this.order.set(key, this.capacity - 1);
    }

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