```typescript
export class LruCache<K, V> {
  /**
   * Creates a new instance of `LruCache`.
   *
   * @param capacity The maximum number of entries in the cache. Must be a positive integer.
   * @throws {Error} If `capacity` is not a positive integer.
   */
  constructor(capacity: number) {
    if (typeof capacity !== 'number' || capacity <= 0) {
      throw new Error('Capacity must be a positive integer');
    }

    this.capacity = capacity;
    this.cache: Map<K, { key: K; value: V; timestamp: number } | undefined>[] = [];
    this.size = 0;
  }

  /**
   * Returns the value associated with the given `key`, or `undefined` if it is not in the cache.
   *
   * @param key The key to look up in the cache.
   * @returns The value associated with the given `key`, or `undefined`.
   */
  get(key: K): V | undefined {
    const index = this.cache.findIndex((entry) => entry?.key === key);
    if (index !== -1) {
      const entry = this.cache[index][0];
      // Remove item from cache, and push to end so it's most recently used
      this.cache.splice(index, 1);
      this.cache.push([entry, entry.timestamp + 1]);
      return entry.value;
    }
    // If item is not in cache, do nothing, no use of key.
    return undefined;
  }

  /**
   * Sets the value associated with the given `key` to the given `value`.
   *
   * @param key The key to set in the cache.
   * @param value The value to set for the given `key`.
   */
  set(key: K, value: V): void {
    const index = this.cache.findIndex((entry) => entry?.key === key);
    if (index !== -1) {
      // If item is already in cache, remove it and re-add to end so it's most recently used
      const entry = this.cache[index][0];
      entry.value = value;
      this.cache.splice(index, 1);
      this.cache.push([entry, entry.timestamp + 1]);
    } else {
      // If item is not in cache, add it to the end
      if (this.size >= this.capacity) {
        const oldestEntry = this.cache.pop()[0];
        this.size--;
        delete oldestEntry.value;
      }
      this.cache.push([[key, value], Date.now()]);
      this.size++;
    }
  }

  /**
   * Returns the number of entries in the cache.
   *
   * @returns The number of entries in the cache.
   */
  get size(): number {
    return this.size;
  }
}
```