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

  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();
    this.timestamps = [];
  }

  get(key: K): V | undefined {
    const entry = this.cache.get(key);
    if (entry) {
      this.updateTimestamp(key, entry.timestamp);
      return entry.value;
    }
    return undefined;
  }

  set(key: K, value: V): void {
    if (this.cache.has(key)) {
      this.cache.set(key, { value, timestamp: Date.now() });
    } else {
      this.cache.set(key, { value, timestamp: Date.now() });
      this.timestamps.push({ key, timestamp: Date.now() });
    }
    if (this.cache.size > this.capacity) {
      const oldest = this.timestamps.shift();
      if (oldest) {
        this.cache.delete(oldest.key);
      }
    }
    this.updateTimestamp(key, Date.now());
  }

  private updateTimestamp(key: K, timestamp: number): void {
    const index = this.timestamps.findIndex((entry) => entry.key === key);
    if (index !== -1) {
      this.timestamps.splice(index, 1);
    }
    this.timestamps.push({ key, timestamp });
  }

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