ladybird/Libraries/LibHTTP/Cache/DiskCache.h
Timothy Flynn d773ba25cf LibHTTP: Impose a limit on the total disk cache size
Rather than letting our disk cache grow unbounded, let's impose a limit
on the estimated total disk cache size. The limits chosen are vaguely
inspired by Chromium.

We impose a total disk cache limit of 5 GiB. Chromium imposes an overall
limit of 1.25 GiB; I've chosen more here because we currently cache
uncompressed data from cURL.

The limit is further restricted by the amount of available disk space,
which we just check once at startup (as does Chromium). We will choose a
percentage of the free space available on systems with limited space.

Our eviction errs on the side of simplicity. We will remove the least
recently accessed entries until the total estimated cache size does not
exceed our limit. This could potentially be improved in the future. For
example, if the next entry to consider is 40 MiB, and we only need to
free 1 MiB of space, we could try evicting slightly more recently used
entries. This would prevent evicting more than we need to.
2026-02-13 10:20:52 -05:00

90 lines
2.6 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/LexicalPath.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <LibDatabase/Database.h>
#include <LibHTTP/Cache/CacheEntry.h>
#include <LibHTTP/Cache/CacheIndex.h>
#include <LibHTTP/Cache/CacheMode.h>
#include <LibURL/Forward.h>
namespace HTTP {
class DiskCache {
public:
enum class Mode {
Normal,
// In partitioned mode, the cache is enabled as normal, but each RequestServer process operates with a unique
// disk cache database.
Partitioned,
// In test mode, we only enable caching of responses on a per-request basis, signified by a request header. The
// response headers will include some status on how the request was handled.
Testing,
};
static ErrorOr<DiskCache> create(Mode);
DiskCache(DiskCache&&);
DiskCache& operator=(DiskCache&&);
~DiskCache();
Mode mode() const { return m_mode; }
struct CacheHasOpenEntry { };
Variant<Optional<CacheEntryWriter&>, CacheHasOpenEntry> create_entry(CacheRequest&, URL::URL const&, StringView method, HeaderList const& request_headers, UnixDateTime request_start_time);
enum class OpenMode {
Read,
Revalidate,
};
Variant<Optional<CacheEntryReader&>, CacheHasOpenEntry> open_entry(CacheRequest&, URL::URL const&, StringView method, HeaderList const& request_headers, CacheMode, OpenMode);
void remove_entries_exceeding_cache_limit();
Requests::CacheSizes estimate_cache_size_accessed_since(UnixDateTime since);
void remove_entries_accessed_since(UnixDateTime since);
LexicalPath const& cache_directory() const { return m_cache_directory; }
void cache_entry_closed(Badge<CacheEntry>, CacheEntry const&);
private:
DiskCache(Mode, NonnullRefPtr<Database::Database>, LexicalPath cache_directory, CacheIndex);
enum class CheckReaderEntries {
No,
Yes,
};
bool check_if_cache_has_open_entry(CacheRequest&, u64 cache_key, URL::URL const&, CheckReaderEntries);
void delete_entry(u64 cache_key, u64 vary_key);
Mode m_mode;
NonnullRefPtr<Database::Database> m_database;
struct OpenCacheEntry {
NonnullOwnPtr<CacheEntry> entry;
WeakPtr<CacheRequest> request;
};
HashMap<u64, Vector<OpenCacheEntry, 1>> m_open_cache_entries;
HashMap<u64, Vector<WeakPtr<CacheRequest>, 1>> m_requests_waiting_completion;
LexicalPath m_cache_directory;
CacheIndex m_index;
};
}