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

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

    /**
     * Returns the value associated with the given key, or undefined if no such entry exists.
     * The cache is updated and counts as a use of the cached entry.
     */
    get(key: K): V | undefined {
        const entryIndex = this.index.get(key);
        if (entryIndex === undefined) {
            return undefined;
        }
        const [value, timestamp] = this.cache.get(key)!;
        // Update the value and update index only when not a read operation
        if (entryIndex !== 0 && !this.isRead) {
            this.cache.set(key, [value, ++timestamp]);
            this.index.set(key, 1);
        }
        return value;
    }

    /**
     * Inserts or updates the given key-value pair in the cache.
     * The cache is updated and counts as a use of the cached entry.
     */
    set(key: K, value: V): void {
        if (this.cache.has(key)) {
            this.cache.get(key)![1]++;
        } else {
            if (this.cache.size >= this.capacity) {
                const [lruKey, lruTimestamp] = Array.from(this.cache.entries()).sort((a, b) => b[1])[0];
                this.cache.delete(lruKey);
                this.index.delete(lruKey);
            }
        }
        this.cache.set(key, [value, 1]);
        this.index.set(key, 0);
    }

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

    private isRead = true; // flag to track if a read operation
}
```