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

  constructor(capacity: number) {
    if (!Number.isPositiveInteger(capacity)) {
      throw new Error('Capacity must be a positive integer');
    }
    this.capacity = capacity;
    this.cache = new Map();
    this.order = [];
  }

  get(key: K): V | undefined {
    const value = this.cache.get(key);
    if (value !== undefined) {
      // Move the key to the end of the order list to mark it as recently used
      this.updateOrder(key);
    }
    return value;
  }

  set(key: K, value: V): void {
    if (!this.cache.has(key)) {
      if (this.order.length >= this.capacity) {
        // Evict the least recently used item
        const lruKey = this.order.shift();
        if (lruKey !== undefined) {
          this.cache.delete(lruKey);
        }
      }
      this.order.push(key);
    } else {
      // Update the order since the key is being set again
      this.updateOrder(key);
    }
    this.cache.set(key, value);
  }

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

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