```ts
export class LruCache<K, V> {
  private readonly capacity: number;
  private readonly cache: Map<K, { value: V; timestamp: number }>;
  private timeCounter = 0;

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

  get(key: K): V | undefined {
    const now = ++this.timeCounter;
    const item = this.cache.get(key);
    if (item !== undefined) {
      item.timestamp = now;
      return item.value;
    }
    return undefined;
  }

  set(key: K, value: V): void {
    const now = ++this.timeCounter;
    if (this.cache.has(key)) {
      // Update existing key
      this.cache.get(key)!.timestamp = now;
    } else {
      // Add new key
      if (this.cache.size >= this.capacity) {
        // Evict the least recently used entry
        const oldestEntry = minBy(this.cache.values(), (entry) => entry.timestamp)!;
        this.cache.delete(oldestEntry.value.key);
      }
    }
    this.cache.set(key, { value, timestamp: now });
  }

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

function minBy<T>(array: T[], selector: (value: T) => unknown): T | undefined {
  let result: T | undefined = undefined;
  for (const item of array) {
    if (result === undefined || selector(item) < selector(result)) {
      result = item;
    }
  }
  return result;
}
```