```typescript
/**
 * A fixed-capacity least-recently-used cache.
 */
export class LruCache<K, V> {
  private capacity: number;
  private map: Map<K, { value: V; index: number }>;
  private keys: K[];

  /**
   * Creates a new LruCache instance with the given capacity.
   *
   * @param capacity The maximum size of the cache.
   */
  constructor(capacity: number) {
    if (!(0..capacity).includes(capacity)) {
      throw new Error('Capacity must be a positive integer.');
    }
    this.capacity = capacity;
    this.map = new Map();
    this.keys = [];
  }

  /**
   * Returns the value associated with the given key, or undefined if no such key exists.
   *
   * @param key The key to look up in the cache.
   */
  get(key: K): V | undefined {
    const entry = this.map.get(key);
    if (!entry) return undefined;
    const index = this.keys.indexOf(key);
    if (index === -1) throw new Error(`Key not found.`);
    this.keys.splice(index, 1);
    this.keys.push(key);
    return entry.value;
  }

  /**
   * Inserts or updates the value associated with the given key in the cache.
   *
   * @param key The key to insert or update.
   * @param value The new value to associate with the key.
   */
  set(key: K, value: V) {
    const entry = this.map.get(key);
    if (entry && entry.value === value) return;
    if (this.keys.includes(key)) this.keys.splice(this.keys.indexOf(key), 1);
    if (this.map.size >= this.capacity) {
      const oldestKey = this.keys.shift() as K;
      this.map.delete(oldestKey);
    }
    this.map.set(key, { value, index: this.keys.length });
    this.keys.push(key);
  }

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