```typescript
export class LruCache<K, V> {
  constructor(capacity: number) {
    if (typeof capacity !== 'number' || capacity <= 0 || !Number.isInteger(capacity)) {
      throw new Error('Capacity must be a positive integer');
    }
    this.capacity = capacity;
    this.cache = new Map<K, { value: V; timestamp: number }>();
    this.recentlyUsed = new Map<K, K>();
  }

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

  get(key: K): V | undefined {
    if (!this.cache.has(key)) {
      return undefined;
    }
    const { value, timestamp } = this.cache.get(key)!;
    this.recentlyUsed.set(key, key);
    // Update the timestamp to indicate recent use
    this.cache.set(key, { value, timestamp: Date.now() });
    return value;
  }

  set(key: K, value: V): void {
    if (this.cache.has(key)) {
      const { timestamp } = this.cache.get(key)!;
      // Update the timestamp to indicate recent use
      this.cache.set(key, { value, timestamp: Date.now() });
      // Move recently used key to front
      this.recentlyUsed.delete(key);
      this.recentlyUsed.set(key, key);
    } else {
      if (this.cache.size >= this.capacity) {
        // Evict the least recently used entry
        const lruKey = Array.from(this.recentlyUsed.keys()).shift()!;
        this.cache.delete(lruKey);
        this.recentlyUsed.delete(lruKey);
      }
      // Insert new key-value pair
      this.cache.set(key, { value, timestamp: Date.now() });
      this.recentlyUsed.set(key, key);
    }
  }
}
```