mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-04-19 10:20:22 +00:00
We now partition the HTTP disk cache based on the Vary response header. If a cached response contains a Vary header, we look for each of the header names in the outgoing HTTP request. The outgoing request must match every header value in the original request for the cache entry to be used; otherwise, a new request will be issued, and a separate cache entry will be created. Note that we must now defer creating the disk cache file itself until we have received the response headers. The Vary key is computed from these headers, and affects the partitioned disk cache file name. There are further optimizations we can make here. If we have a Vary mismatch, we could find the best candidate cached response and issue a conditional HTTP request. The content server may then respond with an HTTP 304 if the mismatched request headers are actually okay. But for now, if we have a Vary mismatch, we issue an unconditional request as a purely correctness-oriented patch.
71 lines
2.3 KiB
C++
71 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2025-2026, Tim Flynn <trflynn89@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Error.h>
|
|
#include <AK/HashMap.h>
|
|
#include <AK/NonnullRawPtr.h>
|
|
#include <AK/Time.h>
|
|
#include <AK/Types.h>
|
|
#include <LibDatabase/Database.h>
|
|
#include <LibHTTP/HeaderList.h>
|
|
#include <LibRequests/CacheSizes.h>
|
|
|
|
namespace HTTP {
|
|
|
|
// The cache index is a SQL database containing metadata about each cache entry. An entry in the index is created once
|
|
// the entire cache entry has been successfully written to disk.
|
|
class CacheIndex {
|
|
struct Entry {
|
|
u64 vary_key { 0 };
|
|
|
|
String url;
|
|
NonnullRefPtr<HeaderList> request_headers;
|
|
NonnullRefPtr<HeaderList> response_headers;
|
|
u64 data_size { 0 };
|
|
|
|
UnixDateTime request_time;
|
|
UnixDateTime response_time;
|
|
UnixDateTime last_access_time;
|
|
};
|
|
|
|
public:
|
|
static ErrorOr<CacheIndex> create(Database::Database&);
|
|
|
|
void create_entry(u64 cache_key, u64 vary_key, String url, NonnullRefPtr<HeaderList> request_headers, NonnullRefPtr<HeaderList> response_headers, u64 data_size, UnixDateTime request_time, UnixDateTime response_time);
|
|
void remove_entry(u64 cache_key, u64 vary_key);
|
|
void remove_entries_accessed_since(UnixDateTime, Function<void(u64 cache_key, u64 vary_key)> on_entry_removed);
|
|
|
|
Optional<Entry const&> find_entry(u64 cache_key, HeaderList const& request_headers);
|
|
|
|
void update_response_headers(u64 cache_key, u64 vary_key, NonnullRefPtr<HeaderList>);
|
|
void update_last_access_time(u64 cache_key, u64 vary_key);
|
|
|
|
Requests::CacheSizes estimate_cache_size_accessed_since(UnixDateTime since);
|
|
|
|
private:
|
|
struct Statements {
|
|
Database::StatementID insert_entry { 0 };
|
|
Database::StatementID remove_entry { 0 };
|
|
Database::StatementID remove_entries_accessed_since { 0 };
|
|
Database::StatementID select_entries { 0 };
|
|
Database::StatementID update_response_headers { 0 };
|
|
Database::StatementID update_last_access_time { 0 };
|
|
Database::StatementID estimate_cache_size_accessed_since { 0 };
|
|
};
|
|
|
|
CacheIndex(Database::Database&, Statements);
|
|
|
|
Optional<Entry&> get_entry(u64 cache_key, u64 vary_key);
|
|
|
|
NonnullRawPtr<Database::Database> m_database;
|
|
Statements m_statements;
|
|
|
|
HashMap<u64, Vector<Entry>> m_entries;
|
|
};
|
|
|
|
}
|