```ts
export class LruCache<K, V> {
  private readonly capacity: number;
  private readonly cache: Map<K, V> = new Map();
  private readonly usageOrder: K[] = [];

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

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

    // Move to end (most recently used)
    const index = this.usageOrder.indexOf(key);
    this.usageOrder.splice(index, 1);
    this.usageOrder.push(key);

    return this.cache.get(key)!;
  }

  set(key: K, value: V): void {
    if (this.cache.has(key)) {
      // Update existing key
      const index = this.usageOrder.indexOf(key);
      this.usageOrder.splice(index, 1);
      this.usageOrder.push(key);
      this.cache.set(key, value);
    } else {
      // Add new key
      if (this.cache.size >= this.capacity) {
        // Remove least recently used
        const lruKey = this.usageOrder.shift();
        if (lruKey !== undefined) {
          this.cache.delete(lruKey);
        }
      }
      this.cache.set(key, value);
      this.usageOrder.push(key);
    }
  }

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