ImageCache last use time


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 lastUseTime is only reset on the message thread based on the reference count in the image, this will always return 1 as I’m not holding onto a reference to the image (and I would really prefer not to or it would essentially defeat the point of the ImageCache in this case). However is seems to me that when you call any of the getter methods and the image is returned you are surely doing that to use it, therefore lastUseTime should be reset no?


The only problem I see is, that you might access your images from arbitrary threads. Therefore writing to lastUseTime comes from different threads and reading from the timer is done from the message thread.
So either the lastUseTime needs to be wrapped atomic or accessing the image will be limited to the message thread…


Yeah I had thought of that too, I think having lastUseTime be atomic shouldn’t be a big issue, but I thought I would see if others agree with the logic first - no point trying to solve something that doesn’t need solving.


Actually I’ve had a closer look and there is already a lock. It is obtained both before adding the item to the array, and in getFromHashCode(), therfore adding…
item->lastUseTime = Time::getApproximateMillisecondCounter(); in Pimpl::getFromHashCode()
…should be all that is required.


Thanks, I’ll take a look at this…