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

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

  get(key: K): V | undefined {
    const entry = this.cache.get(key);
    if (!entry) return undefined;

    // Update the timestamp to mark as recently used
    entry.timestamp = Date.now();
    return entry.value;
  }

  set(key: K, value: V): void {
    const timestamp = Date.now();

    // If key exists, update its value and timestamp
    if (this.cache.has(key)) {
      this.cache.get(key)!.value = value;
      this.cache.get(key)!.timestamp = timestamp;
      return;
    }

    // If cache is at capacity, evict the least recently used item
    if (this.cache.size >= this.capacity) {
      let leastUsedKey: K | undefined = undefined;
      let minTimestamp = Infinity;

      for (const [k, { timestamp }] of this.cache.entries()) {
        if (timestamp < minTimestamp) {
          minTimestamp = timestamp;
          leastUsedKey = k;
        }
      }

      if (leastUsedKey !== undefined) {
        this.cache.delete(leastUsedKey);
      }
    }

    // Add the new key-value pair
    this.cache.set(key, { value, timestamp });
  }

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