# What is the point of ImageCache if it constantly dumps the images?

**URL:** <https://forum.juce.com/t/what-is-the-point-of-imagecache-if-it-constantly-dumps-the-images/61874>\
**Category:** General JUCE discussion\
**Created:** [June 16, 2024, 10:17pm UTC](https://forum.juce.com/t/what-is-the-point-of-imagecache-if-it-constantly-dumps-the-images/61874 "2024-06-16T22:17:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [June 16, 2024, 10:17pm UTC](https://forum.juce.com/t/what-is-the-point-of-imagecache-if-it-constantly-dumps-the-images/61874/1 "2024-06-16T22:17:11Z")

</div>

I don’t quite understand how this is supposed to work…

I have an App Logo image in the upper corner of my app; I want to create the image one time, and then just get it from the ImageCache when it needs to be redrawn

```auto
int dataSize = 0;
const char* resourceName = "kmoLogo_png";
 
auto hashCode = (juce::String(resourceName) + "_iconCacheSalt").hashCode64();
auto appIcon = juce::ImageCache::getFromHashCode(hashCode);
if (appIcon.isNull())
{
	const char* imageData = BinaryData::getNamedResource(resourceName, dataSize);
	appIcon = ImageFileFormat::loadFrom(imageData, (size_t) dataSize);
	ImageCache::addImageToCache(appIcon, hashCode);

	DBG(“create new image”);
}

g.drawImageWithin(appIcon, iconRect.getX(), iconRect.getY(), iconRect.getWidth(), iconRect.getHeight(), RectanglePlacement::centred );

```

The problem is, the image is apparently released from the cache after a certain period (by the Timer in ImageCache), and debugging shows it is constantly being recreated  
Am I misunderstanding the concept? Why does the timeout default to removing an image after about 5 seconds? I suppose I could just call `ImageCache::setCacheTimeout()` with some really large integer, but why does it work like this?

Related:

> [@ImageCache last use time](https://forum.juce.com/t/imagecache-last-use-time/21742):
>
> It seems to me that getFromHashCode() in ImageCache::Pimpl should reset lastUseTime. I am adding Images to the ImageCache and not holding on to them, instead I am calculating a hash code on the message thread and call getFromHashCode(). If it returns a null image I generate the image and add it to the cache, otherwise I get a copy draw it and the copy is deleted in scope. The problem is that the Image is guaranteed to be removed from the cache once the cacheTimeout is reached because lastUseTim…

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 17, 2024, 12:41pm UTC](https://forum.juce.com/t/what-is-the-point-of-imagecache-if-it-constantly-dumps-the-images/61874/3 "2024-06-17T12:41:22Z")

</div>

The image data is already in memory (BinaryData), so the normal pattern would be to use the higher level `ImageCache::getFromMemory` vs. interacting directly with the cache, I think. But it’s a good question about the timeout. Is it 5 seconds after the last `juce::Image` goes out of scope?

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [June 17, 2024, 2:10pm UTC](https://forum.juce.com/t/what-is-the-point-of-imagecache-if-it-constantly-dumps-the-images/61874/4 "2024-06-17T14:10:50Z")

</div>

> [@sudara](#):
>
> so the normal pattern would be to use the higher level `ImageCache::getFromMemory` vs. interacting directly with the cache

Actually, I didn’t show it, but the main reason I do it that way is because on Windows I’m using the gin module to resize the image before adding it to the cache, for better results.

```auto
        auto iconRect = getLocalBounds();
        int dataSize = 0;
        const char* resourceName = "kmoLogo_png";

        // since we are using gin (on WINDOWS), we must add and retrieve the image
        // from/to the cache manually with a hashcode, which makes this whole thing more complicated
        auto hashCode = (juce::String(resourceName) + "_iconCacheSalt").hashCode64();
        auto appIcon = juce::ImageCache::getFromHashCode(hashCode);

        if (appIcon.isNull())
        {
            const char* imageData = BinaryData::getNamedResource(resourceName, dataSize);
            
        #if WIN_VERSION // - use gin
            auto ginScaleFactor = 2; 
            auto image = ImageFileFormat::loadFrom(imageData, (size_t) dataSize);
            appIcon = gin::applyResize(image, iconWidth * ginScaleFactor, iconWidth * ginScaleFactor); // double-size
        #else
            appIcon = ImageFileFormat::loadFrom(imageData, (size_t) dataSize);
        #endif //WIN_VERSION
            ImageCache::addImageToCache(appIcon, hashCode);
        }

        g.drawImageWithin(appIcon, iconRect.getX(), iconRect.getY(), iconRect.getWidth(), iconRect.getHeight(), RectanglePlacement::centred );

```

But yes, for some reason the default timeout is 5 seconds and I don’t understand what use case this is for, then… I assumed it was so you don’t have to keep recreating images…

[https://docs.juce.com/master/classImageCache.html#a7e8f2a2260acf7ca4f4a62c36ce99332](https://docs.juce.com/master/classImageCache.html#a7e8f2a2260acf7ca4f4a62c36ce99332)

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [June 17, 2024, 2:38pm UTC](https://forum.juce.com/t/what-is-the-point-of-imagecache-if-it-constantly-dumps-the-images/61874/5 "2024-06-17T14:38:26Z")

</div>

I’m also curious. To me the short timeout and the [history going back 17+ years](https://github.com/juce-framework/JUCE/commits/master/modules/juce_graphics/images/juce_ImageCache.h) indicates that the existence of ImageCache is to resolve a specific internal need (e.g. sharing an underlying image vs. allocating new memory when image is used in more than one place) vs. being a good generalized solution. But I don’t have experience with it directly.

Unless `appIcon` is being used in multiple places, could you keep things simple and persist it as a member variable of the component?
