Skip to content

Commit

Permalink
Merge pull request #1999 from alcomposer/store-image-cache
Browse files Browse the repository at this point in the history
Cache downloaded image from store so users don't have to wait when op…
  • Loading branch information
timothyschoen authored Dec 4, 2024
2 parents 2146b41 + 4c4d826 commit cfaa6a6
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions Source/Dialogs/PatchStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,40 @@ class DownloadPool : public DeletedAtShutdown
void downloadImage(hash32 hash, URL location)
{
imagePool.addJob([this, hash, location](){
auto updateImageListeners = [this](hash32 hash, Image& image) {
MessageManager::callAsync([this, hash, image]() {
for(auto& listener : listeners)
{
listener->imageDownloadCompleted(hash, image);
}
});
};

static std::unordered_map<hash32, Image> downloadImageCache;
static std::mutex cacheMutex;

{
std::lock_guard<std::mutex> lock(cacheMutex);

if (downloadImageCache.contains(hash)) {
if (auto img = downloadImageCache[hash]; img.isValid()) {
updateImageListeners(hash, img);
return;
}
}
}

MemoryBlock block;
// Load the image data from the URL
WebInputStream memstream(location, false);
memstream.readIntoMemoryBlock(block);
auto image = ImageFileFormat::loadFrom(block.getData(), block.getSize());
MessageManager::callAsync([this, hash, image](){
for(auto& listener : listeners)
{
listener->imageDownloadCompleted(hash, image);
}
});
{
std::lock_guard<std::mutex> lock(cacheMutex);

downloadImageCache[hash] = image;
}
updateImageListeners(hash, image);
});
}

Expand Down

0 comments on commit cfaa6a6

Please sign in to comment.