```typescript
class LruCache<K, V> {
  private capacity: number;
  private cache: 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;
    this.cache = new Map();
    this.keys = [];
  }

  get(key: K): V | undefined {
    const value = this.cache.get(key);
    if (value !== undefined) {
      // Move the key to the end of the keys array
      const index = this.keys.indexOf(key);
      this.keys.splice(index, 1);
      this.keys.push(key);
    }
    return value;
  }

  set(key: K, value: V): void {
    if (this.cache.has(key)) {
      // Update existing key and move to the end of keys array
      const index = this.keys.indexOf(key);
      this.keys.splice(index, 1);
    } else if (this.cache.size >= this.capacity) {
      // Evict the least recently used entry
      const oldestKey = this.keys.shift();
      if (oldestKey !== undefined) {
        this.cache.delete(oldestKey);
      }
    }
    // Add new key and value to cache
    this.cache.set(key, value);
    this.keys.push(key);
  }

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