@@ -44,37 +44,35 @@ static constexpr StringView cache_directory_for_mode(DiskCache::Mode mode)
4444 switch (mode) {
4545 case DiskCache::Mode::Normal:
4646 return " Cache" sv;
47- case DiskCache::Mode::Partitioned:
48- // FIXME: Ideally, we could support multiple RequestServer processes using the same database by setting a
49- // reasonable busy timeout. We would also have to prevent multiple processes writing to the same cache
50- // entry file at the same time with some interprocess locking mechanism.
51- return " PartitionedCache" sv;
5247 case DiskCache::Mode::Testing:
5348 return " TestCache" sv;
5449 }
5550 VERIFY_NOT_REACHED ();
5651}
5752
58- ErrorOr<DiskCache> DiskCache::create (Mode mode, LexicalPath cache_root)
53+ ErrorOr<Optional< DiskCache>> DiskCache::create (Mode mode, LexicalPath const & cache_root)
5954{
6055 auto cache_directory = cache_root.append (cache_directory_for_mode (mode));
56+
57+ // Start with a clean slate in test mode. The index is in-memory, but the cache files themselves are leftover.
58+ if (mode == Mode::Testing)
59+ (void )FileSystem::remove (cache_directory.string (), FileSystem::RecursionMode::Allowed);
60+
6161 TRY (Core::Directory::create (cache_directory, Core::Directory::CreateDirectories::Yes));
6262
6363 auto database = mode == DiskCache::Mode::Normal
6464 ? TRY (Database::Database::create (cache_directory.string (), INDEX_DATABASE ))
6565 : TRY (Database::Database::create_memory_backed ());
6666
6767 if (TRY (CacheIndex::migrate_schema (*database)) == Database::MigrationOutcome::DatabaseTooNew) {
68- // Entry file names are derived from the cache key, so writing into the existing cache
69- // directory without its index would corrupt entries the newer build's index refers to.
70- // Partitioned mode is already a per-process transient cache in its own directory.
71- // (A fresh memory-backed database always migrates, so this is necessarily Mode::Normal.)
72- dbgln (" Disk cache index was created by a newer Ladybird version; using a transient cache this session" );
73- return create (Mode::Partitioned, move (cache_root));
68+ // Entry file names are derived from the cache key, so writing into the existing cache directory without its
69+ // index would corrupt entries the newer build's index refers to. (A fresh memory-backed database always
70+ // migrates, so this is necessarily Mode::Normal.)
71+ dbgln (" Disk cache index was created by a newer Ladybird version; disabling disk cache for this session" );
72+ return OptionalNone {};
7473 }
7574
7675 auto index = TRY (CacheIndex::create (database, cache_directory));
77-
7876 return DiskCache { mode, move (database), move (cache_directory), move (index) };
7977}
8078
@@ -84,19 +82,12 @@ DiskCache::DiskCache(Mode mode, NonnullRefPtr<Database::Database> database, Lexi
8482 , m_cache_directory(move(cache_directory))
8583 , m_index(move(index))
8684{
87- if (m_mode == Mode::Partitioned)
88- m_partitioned_cache_key = String::number (Core::System::getpid ());
8985}
9086
9187DiskCache::DiskCache (DiskCache&&) = default ;
9288DiskCache& DiskCache::operator =(DiskCache&&) = default ;
9389
94- DiskCache::~DiskCache ()
95- {
96- // Clean up cache directories in testing modes to prevent endless growth of disk usage.
97- if (m_mode != Mode::Normal)
98- remove_entries_accessed_since (UnixDateTime::earliest ());
99- }
90+ DiskCache::~DiskCache () = default ;
10091
10192Variant<Optional<CacheEntryWriter&>, DiskCache::CacheHasOpenEntry> DiskCache::create_entry (CacheRequest& request, URL ::URL const & url, StringView method, HeaderList const & request_headers, UnixDateTime request_start_time)
10293{
@@ -109,7 +100,7 @@ Variant<Optional<CacheEntryWriter&>, DiskCache::CacheHasOpenEntry> DiskCache::cr
109100 }
110101
111102 auto serialized_url = serialize_url_for_cache_storage (url);
112- auto cache_key = create_cache_key (serialized_url, method, m_partitioned_cache_key );
103+ auto cache_key = create_cache_key (serialized_url, method);
113104
114105 if (check_if_cache_has_open_entry (request, cache_key, url, CheckReaderEntries::Yes))
115106 return CacheHasOpenEntry {};
@@ -139,7 +130,7 @@ Variant<Optional<CacheEntryReader&>, DiskCache::CacheHasOpenEntry> DiskCache::op
139130 return Optional<CacheEntryReader&> {};
140131
141132 auto serialized_url = serialize_url_for_cache_storage (url);
142- auto cache_key = create_cache_key (serialized_url, method, m_partitioned_cache_key );
133+ auto cache_key = create_cache_key (serialized_url, method);
143134
144135 if (check_if_cache_has_open_entry (request, cache_key, url, open_mode == OpenMode::Read ? CheckReaderEntries::No : CheckReaderEntries::Yes))
145136 return CacheHasOpenEntry {};
@@ -238,7 +229,7 @@ ErrorOr<bool> DiskCache::create_synthetic_entry(URL::URL const& url, StringView
238229 return false ;
239230
240231 auto serialized_url = serialize_url_for_cache_storage (url);
241- auto cache_key = create_cache_key (serialized_url, method, m_partitioned_cache_key );
232+ auto cache_key = create_cache_key (serialized_url, method);
242233 constexpr u64 synthetic_vary_key = 0 ;
243234
244235 if (m_index.has_entry (cache_key, synthetic_vary_key))
@@ -256,7 +247,7 @@ ErrorOr<bool> DiskCache::store_associated_data(URL::URL const& url, StringView m
256247 return false ;
257248
258249 auto serialized_url = serialize_url_for_cache_storage (url);
259- auto cache_key = create_cache_key (serialized_url, method, m_partitioned_cache_key );
250+ auto cache_key = create_cache_key (serialized_url, method);
260251 if (!vary_key.has_value ()) {
261252 auto index_entry = m_index.find_entry (cache_key, request_headers);
262253 if (!index_entry.has_value ())
@@ -291,7 +282,7 @@ ErrorOr<Optional<ByteBuffer>> DiskCache::retrieve_associated_data(URL::URL const
291282 return Optional<ByteBuffer> {};
292283
293284 auto serialized_url = serialize_url_for_cache_storage (url);
294- auto cache_key = create_cache_key (serialized_url, method, m_partitioned_cache_key );
285+ auto cache_key = create_cache_key (serialized_url, method);
295286 if (!vary_key.has_value ()) {
296287 auto index_entry = m_index.find_entry (cache_key, request_headers);
297288 if (!index_entry.has_value ())
@@ -319,7 +310,7 @@ ErrorOr<Optional<CacheEntryBodyFile>> DiskCache::retrieve_associated_data_file(U
319310 return Optional<CacheEntryBodyFile> {};
320311
321312 auto serialized_url = serialize_url_for_cache_storage (url);
322- auto cache_key = create_cache_key (serialized_url, method, m_partitioned_cache_key );
313+ auto cache_key = create_cache_key (serialized_url, method);
323314 if (!vary_key.has_value ()) {
324315 auto index_entry = m_index.find_entry (cache_key, request_headers);
325316 if (!index_entry.has_value ())
0 commit comments