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

  /**
   * Creates an instance of LruCache.
   * 
   * @param capacity The maximum number of entries in the cache.
   */
  constructor(capacity: number) {
    if (typeof capacity !== 'number' || capacity <= 0) {
      throw new Error('Capacity must be a positive integer.');
    }
    
    this.capacity = capacity;
    this.map = new Map();
    this.order = [];
  }

  /**
   * Returns the value associated with the given key, or undefined if no such entry exists.
   * 
   * @param key The key to look up in the cache.
   */
  get(key: K): V | undefined {
    const entry = this.map.get(key);
    
    if (entry) {
      const [value, index] = entry;
      
      // move the accessed entry to the end of the order array
      this.order.splice(this.order.indexOf(key), 1);
      this.order.push(key);
      
      return value;
    }
    
    return undefined;
  }

  /**
   * Inserts or updates the given key-value pair in the cache.
   * 
   * @param key The key to insert or update.
   * @param value The value associated with the key.
   */
  set(key: K, value: V): void {
    if (this.map.has(key)) {
      this.remove(key);
    }
    
    const index = this.order.indexOf(key);
    
    if (index >= 0) {
      // move to end of order array
      this.order.splice(index, 1);
    } else {
      // key not in order array, so the list is full
      if (this.map.size === this.capacity) {
        const lruKey = this.order.shift() as K;
        this.remove(lruKey);
      }
    }
    
    this.map.set(key, [value, this.order.length - 1]);
    this.order.push(key);
  }

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

  private remove(key: K): void {
    const entry = this.map.get(key);
    
    if (entry) {
      const [value, index] = entry;
      
      // move to end of order array
      this.order.splice(this.order.indexOf(value), 1);
      this.order.push(key);
      
      this.map.delete(key);
    }
  }
}
```