```ts
export class LruCache<K, V> {
  private capacity: number;
  private cache: Record<string, { value: V; lastUsed: number }>;

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

  get(key: K): V | undefined {
    if (this.cache[key]) {
      this.updateLastUsed(key);
      return this.cache[key].value;
    } else {
      return undefined;
    }
  }

  set(key: K, value: V): void {
    this.updateLastUsed(key);
    if (Object.keys(this.cache).length >= this.capacity) {
      const toEvict = Object.keys(this.cache).reduce((minKey, currKey) => {
        return !this.cache[currKey].lastUsed || this.cache[currKey].lastUsed < this.cache[minKey].lastUsed ? currKey : minKey;
      }, '');
      delete this.cache[toEvict];
    }
    this.cache[key] = { value, lastUsed: Date.now() };
  }

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

  private updateLastUsed(key: K) {
    if (this.cache[key]) {
      this.cache[key].lastUsed = Date.now();
    } else {
      throw new Error('Key must exist');
    }
  }
}
```