Files
ladybird/Libraries/LibHTTP/Cache/CacheIndex.h
Aliaksandr Kalenik 14dc9e8ca2 LibHTTP: Short-circuit CacheIndex eviction when under its size limit
remove_entries_exceeding_cache_limit() is called after every network
response, but the cache is usually still under budget and nothing needs
to evict. Every one of those calls currently still runs the
window-function eviction SQL over the whole CacheIndex table just to
conclude there is nothing to do.

Short-circuit the call when the cache is already within its configured
size limit. To make that check cheap, maintain m_total_estimated_size
as a running total of the cache's estimated byte size, so the no-op
case becomes a single u64 compare and the DB is only touched when
there is real work.

Bookkeeping:
- Seed the total in CacheIndex::create() via a new
  select_total_estimated_size statement (COALESCE(..., 0) so an empty
  index returns 0 rather than NULL).
- Each Entry caches serialized_request_headers_size and
  serialized_response_headers_size so we don't re-serialize to
  recompute its footprint; Entry::estimated_size() centralizes the
  arithmetic.
- create_entry() adds the new entry's size. Any row it displaces is
  removed via DELETE ... RETURNING so the total stays accurate even
  for entries that were never loaded into m_entries.
- remove_entry() and the bulk DELETE statements were extended with
  the same RETURNING clause for the same reason.
- update_response_headers() shifts the total by the signed delta
  between old and new serialized header size.

Also COALESCEs estimate_cache_size_accessed_since over an empty table
to 0 so callers don't have to special-case NULL.
2026-04-19 01:31:37 +02:00

94 lines
3.2 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 };
u64 serialized_request_headers_size { 0 };
u64 serialized_response_headers_size { 0 };
UnixDateTime request_time;
UnixDateTime response_time;
UnixDateTime last_access_time;
u64 estimated_size() const
{
return data_size + serialized_request_headers_size + serialized_response_headers_size;
}
};
public:
static ErrorOr<CacheIndex> create(Database::Database&, LexicalPath const& cache_directory);
ErrorOr<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_exceeding_cache_limit(Function<void(u64 cache_key, u64 vary_key)> on_entry_removed);
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);
void set_maximum_disk_cache_size(u64 maximum_disk_cache_size);
private:
struct Statements {
Database::StatementID insert_entry { 0 };
Database::StatementID remove_entry { 0 };
Database::StatementID remove_entries_exceeding_cache_limit { 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 };
Database::StatementID select_total_estimated_size { 0 };
};
struct Limits {
u64 free_disk_space { 0 };
u64 maximum_disk_cache_size { 0 };
u64 maximum_disk_cache_entry_size { 0 };
};
CacheIndex(Database::Database&, Statements, Limits, u64 total_estimated_size);
Optional<Entry&> get_entry(u64 cache_key, u64 vary_key);
void delete_entry(u64 cache_key, u64 vary_key);
NonnullRawPtr<Database::Database> m_database;
Statements m_statements;
HashMap<u64, Vector<Entry>, IdentityHashTraits<u64>> m_entries;
Limits m_limits;
u64 m_total_estimated_size { 0 };
};
}