```ts
export class LruCache<K, V> {
    private cache: { [key: K]: V } = {};
    private order: K[] = [];
    private capacity: number;

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

    get(key: K): V | undefined {
        const value = this.cache[key];
        if (value !== undefined) {
            // Move the accessed key to the front of the order array
            const index = this.order.indexOf(key);
            if (index !== -1) {
                this.order.splice(index, 1);
                this.order.unshift(key);
            }
        }
        return value;
    }

    set(key: K, value: V): void {
        // Remove the least recently used key if capacity is exceeded
        if (this.size() >= this.capacity) {
            const leastRecentlyUsedKey = this.order.pop();
            delete this.cache[leastRecentlyUsedKey];
        }
        // Add or update the new key-value pair
        this.cache[key] = value;
        this.order.push(key);
    }

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