```ts
export class LruCache<K, V> {
  private cache: Map<K, { value: V; useCount: number }> = new Map();
  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 entry = this.cache.get(key);
    if (!entry) return undefined;

    entry.useCount++;
    return entry.value;
  }

  set(key: K, value: V): void {
    const entry = this.cache.get(key);

    if (entry) {
      entry.value = value;
      entry.useCount++;
    } else {
      if (this.cache.size >= this.capacity) {
        const oldestEntry = Array.from(this.cache.values()).reduce((a, b) => a.useCount < b.useCount ? a : b);
        this.cache.delete(oldestEntry.key);
      }
      this.cache.set(key, { value, useCount: 1 });
    }
  }

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