```ts
export class LruCache<K, V> {
  private capacity: number;
  private cache = new Map<K, V>();
  private keys: K[] = [];

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

  get(key: K): V | undefined {
    if (this.cache.has(key)) {
      const index = this.keys.findIndex(k => k === key);
      this.keys.splice(index, 1);
      this.keys.push(key);
      return this.cache.get(key);
    }
    return undefined;
  }

  set(key: K, value: V): void {
    if (this.cache.has(key)) {
      const index = this.keys.findIndex(k => k === key);
      this.keys.splice(index, 1);
    } else if (this.cache.size >= this.capacity) {
      const oldestKey = this.keys.shift();
      if (oldestKey !== undefined) {
        this.cache.delete(oldestKey);
      }
    }
    this.cache.set(key, value);
    this.keys.push(key);
  }

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