evictLeastUsedItem()

in src/application/ui/src/components/ProductImage.js [111:155]


    evictLeastUsedItem() {
        // Keep trying items from the start of the array until we find one to evict
        for (let i = 0; i < this.accessOrder.length; i++) {
            const key = this.accessOrder[i];

            // Don't evict visible images
            if (this.visibleImages.has(key)) {
                continue;
            }

            // Don't evict loaded images if there are error or unloaded images available
            if (this.loadedImages.has(key) &&
                (this.errorImages.size > 0 ||
                    this.accessOrder.length - this.loadedImages.size > 0)) {
                continue;
            }

            // Found an item to evict
            this.accessOrder.splice(i, 1);
            this.cache.delete(key);
            this.loadedImages.delete(key);
            this.errorImages.delete(key);
            this.visibleImages.delete(key);

            // Log detailed eviction information when debug mode is enabled
            if (DEBUG_MODE) {
                const status = this.errorImages.has(key) ? "error" :
                    this.loadedImages.has(key) ? "loaded" : "unloaded";
                console.log(`LRU Cache: Evicted image ${key} (${status}, position ${i}/${this.accessOrder.length})`);
            }
            return;
        }

        // If we got here, all images are visible or loaded and we couldn't find one to evict
        // Just evict the oldest
        const key = this.accessOrder.shift();
        this.cache.delete(key);
        this.loadedImages.delete(key);
        this.errorImages.delete(key);
        this.visibleImages.delete(key);

        if (DEBUG_MODE) {
            console.log(`LRU Cache: Forced eviction of image ${key} (oldest)`);
        }
    }