```ts
export class LruCache<K, V> {
  private readonly capacity: number;
  private cacheMap: Map<K, { value: V; recent: boolean }>;
  private usageQueue: K[];

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

  get(key: K): V | undefined {
    const entry = this.cacheMap.get(key);
    if (entry) {
      // Mark as recently used
      this.updateUsage(key);
      return entry.value;
    }
    return undefined;
  }

  set(key: K, value: V): void {
    if (this.cacheMap.has(key)) {
      // Update existing entry
      const entry = this.cacheMap.get(key)!;
      entry.value = value;
      this.updateUsage(key);
    } else {
      // Add new entry
      if (this.cacheMap.size >= this.capacity) {
        this.evict();
      }
      this.cacheMap.set(key, { value, recent: true });
      this.usageQueue.push(key);
    }
  }

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

  private updateUsage(key: K): void {
    const index = this.usageQueue.indexOf(key);
    if (index !== -1) {
      // Move to the end to mark as recently used
      this.usageQueue.splice(index, 1);
      this.usageQueue.push(key);
    }
  }

  private evict(): void {
    const keyToEvict = this.usageQueue.shift();
    if (keyToEvict) {
      this.cacheMap.delete(keyToEvict);
    }
  }
}
```