mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
RequestServer: Create a cache metadata table
This currently just holds a cache version. If we make a breaking change to the cache format, we can increment the version here to wipe the cache database. This is certainly a blunt hammer approach; we will want to handle such changes more gracefully when we can.
This commit is contained in:
parent
426773e8cf
commit
bf7c5cdf07
Notes:
github-actions[bot]
2025-11-02 18:04:42 +00:00
Author: https://github.com/trflynn89
Commit: bf7c5cdf07
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6645
4 changed files with 53 additions and 5 deletions
|
|
@ -232,7 +232,7 @@ ErrorOr<NonnullOwnPtr<CacheEntryReader>> CacheEntryReader::create(DiskCache& dis
|
|||
|
||||
if (cache_header.magic != CacheHeader::CACHE_MAGIC)
|
||||
return Error::from_string_literal("Magic value mismatch");
|
||||
if (cache_header.version != CacheHeader::CACHE_VERSION)
|
||||
if (cache_header.version != CACHE_VERSION)
|
||||
return Error::from_string_literal("Version mismatch");
|
||||
|
||||
url = TRY(String::from_stream(*file, cache_header.url_size));
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
#include <AK/Types.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibHTTP/HeaderMap.h>
|
||||
#include <RequestServer/Cache/Version.h>
|
||||
#include <RequestServer/Forward.h>
|
||||
|
||||
namespace RequestServer {
|
||||
|
|
@ -22,7 +23,6 @@ struct [[gnu::packed]] CacheHeader {
|
|||
ErrorOr<void> write_to_stream(Stream&) const;
|
||||
|
||||
static constexpr auto CACHE_MAGIC = 0xcafef00du;
|
||||
static constexpr auto CACHE_VERSION = 1;
|
||||
|
||||
u32 magic { CACHE_MAGIC };
|
||||
u32 version { CACHE_VERSION };
|
||||
|
|
|
|||
|
|
@ -5,12 +5,43 @@
|
|||
*/
|
||||
|
||||
#include <RequestServer/Cache/CacheIndex.h>
|
||||
#include <RequestServer/Cache/Version.h>
|
||||
|
||||
namespace RequestServer {
|
||||
|
||||
static constexpr u32 CACHE_METADATA_KEY = 12389u;
|
||||
|
||||
ErrorOr<CacheIndex> CacheIndex::create(Database::Database& database)
|
||||
{
|
||||
auto create_table = TRY(database.prepare_statement(R"#(
|
||||
auto create_cache_metadata_table = TRY(database.prepare_statement(R"#(
|
||||
CREATE TABLE IF NOT EXISTS CacheMetadata (
|
||||
metadata_key INTEGER,
|
||||
version INTEGER,
|
||||
PRIMARY KEY(metadata_key)
|
||||
);
|
||||
)#"sv));
|
||||
database.execute_statement(create_cache_metadata_table, {});
|
||||
|
||||
auto read_cache_version = TRY(database.prepare_statement("SELECT version FROM CacheMetadata WHERE metadata_key = ?;"sv));
|
||||
auto cache_version = 0u;
|
||||
|
||||
database.execute_statement(
|
||||
read_cache_version,
|
||||
[&](auto statement_id) { cache_version = database.result_column<u32>(statement_id, 0); },
|
||||
CACHE_METADATA_KEY);
|
||||
|
||||
if (cache_version != CACHE_VERSION) {
|
||||
dbgln("\033[31;1mDisk cache version mismatch:\033[0m stored version = {}, new version = {}", cache_version, CACHE_VERSION);
|
||||
|
||||
// FIXME: We should more elegantly handle minor changes, i.e. use ALTER TABLE to add fields to CacheIndex.
|
||||
auto delete_cache_index_table = TRY(database.prepare_statement("DROP TABLE IF EXISTS CacheIndex;"sv));
|
||||
database.execute_statement(delete_cache_index_table, {});
|
||||
|
||||
auto set_cache_version = TRY(database.prepare_statement("INSERT OR REPLACE INTO CacheMetadata VALUES (?, ?);"sv));
|
||||
database.execute_statement(set_cache_version, {}, CACHE_METADATA_KEY, CACHE_VERSION);
|
||||
}
|
||||
|
||||
auto create_cache_index_table = TRY(database.prepare_statement(R"#(
|
||||
CREATE TABLE IF NOT EXISTS CacheIndex (
|
||||
cache_key INTEGER,
|
||||
url TEXT,
|
||||
|
|
@ -19,8 +50,9 @@ ErrorOr<CacheIndex> CacheIndex::create(Database::Database& database)
|
|||
response_time INTEGER,
|
||||
last_access_time INTEGER,
|
||||
PRIMARY KEY(cache_key)
|
||||
);)#"sv));
|
||||
database.execute_statement(create_table, {});
|
||||
);
|
||||
)#"sv));
|
||||
database.execute_statement(create_cache_index_table, {});
|
||||
|
||||
Statements statements {};
|
||||
statements.insert_entry = TRY(database.prepare_statement("INSERT OR REPLACE INTO CacheIndex VALUES (?, ?, ?, ?, ?, ?);"sv));
|
||||
|
|
|
|||
16
Services/RequestServer/Cache/Version.h
Normal file
16
Services/RequestServer/Cache/Version.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace RequestServer {
|
||||
|
||||
// Increment this version when a breaking change is made to the cache index or cache entry formats.
|
||||
static constexpr inline u32 CACHE_VERSION = 1u;
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue