1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

LibWeb: Add a naive Resource cache

This patch introduces a caching mechanism in ResourceLoader. It's keyed
on a LoadRequest object which is what you provide to load_resource()
when you want to load a resource.

We currently never prune the cache, so resources will stay in there
forever. This is obviously not gonna stay that way, but we're just
getting started here. :^)

This should drastically reduce the number of requests when loading
some sites (like Twitter) that reuse the same images over and over.
This commit is contained in:
Andreas Kling 2020-06-01 21:58:29 +02:00
parent f249f07699
commit 7af337764e
7 changed files with 94 additions and 15 deletions

View file

@ -31,6 +31,7 @@
#include <LibCore/File.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
#include <LibWeb/Loader/LoadRequest.h>
#include <LibWeb/Loader/Resource.h>
#include <LibWeb/Loader/ResourceLoader.h>
@ -69,15 +70,25 @@ void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&,
loop.exec();
}
RefPtr<Resource> ResourceLoader::load_resource(const URL& url)
static HashMap<LoadRequest, NonnullRefPtr<Resource>> s_resource_cache;
RefPtr<Resource> ResourceLoader::load_resource(const LoadRequest& request)
{
if (!url.is_valid())
if (!request.is_valid())
return nullptr;
auto resource = Resource::create({}, url);
auto it = s_resource_cache.find(request);
if (it != s_resource_cache.end()) {
dbg() << "Reusing cached resource for: " << request.url();
return it->value;
}
auto resource = Resource::create({}, request);
s_resource_cache.set(request, resource);
load(
url,
request.url(),
[=](auto& data, auto& headers) {
const_cast<Resource&>(*resource).did_load({}, data, headers);
},