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

    /**
     * Creates a new LruCache instance.
     *
     * @param capacity The maximum number of entries in the cache.
     */
    constructor(capacity: number) {
        if (capacity <= 0) {
            throw new Error('Capacity must be a positive integer.');
        }

        this.capacity = capacity;
        this.map = new Map();
        this.index = new Map();
    }

    /**
     * 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 [value, index] = this.map.get(key);
        if (value === undefined) return undefined;

        this.index.delete(index);
        this.index.set(this.size(), key);

        this.map.set(key, [value, this.size()]);
        return value;
    }

    /**
     * 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.get(key);
        } else {
            if (this.size() >= this.capacity) {
                const [leastRecentlyUsedKey] = this.index.keys().next().value;
                this.map.delete(leastRecentlyUsedKey);
                this.index.delete(this.index.get(leastRecentlyUsedKey)!);
            }
        }

        this.map.set(key, [value, this.size()]);
        this.index.set(this.size(), key);
    }

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