LibRequests+RequestServer: Add a method to estimate disk cache size

This allows estimating the cache size stored on disk since a provided
time stamp, and in total.
This commit is contained in:
Timothy Flynn 2025-11-02 13:10:27 -05:00 committed by Tim Flynn
parent d5c00a493c
commit ba49942b6d
Notes: github-actions[bot] 2025-11-12 14:09:00 +00:00
14 changed files with 138 additions and 3 deletions

View file

@ -101,6 +101,7 @@ ErrorOr<CacheIndex> CacheIndex::create(Database::Database& database)
statements.select_entry = TRY(database.prepare_statement("SELECT * FROM CacheIndex WHERE cache_key = ?;"sv));
statements.update_response_headers = TRY(database.prepare_statement("UPDATE CacheIndex SET response_headers = ? WHERE cache_key = ?;"sv));
statements.update_last_access_time = TRY(database.prepare_statement("UPDATE CacheIndex SET last_access_time = ? WHERE cache_key = ?;"sv));
statements.estimate_cache_size_accessed_since = TRY(database.prepare_statement("SELECT SUM(data_size) + SUM(OCTET_LENGTH(response_headers)) FROM CacheIndex WHERE last_access_time >= ?;"sv));
return CacheIndex { database, statements };
}
@ -188,4 +189,21 @@ Optional<CacheIndex::Entry&> CacheIndex::find_entry(u64 cache_key)
return m_entries.get(cache_key);
}
Requests::CacheSizes CacheIndex::estimate_cache_size_accessed_since(UnixDateTime since) const
{
Requests::CacheSizes sizes;
m_database.execute_statement(
m_statements.estimate_cache_size_accessed_since,
[&](auto statement_id) { sizes.since_requested_time = m_database.result_column<u64>(statement_id, 0); },
since);
m_database.execute_statement(
m_statements.estimate_cache_size_accessed_since,
[&](auto statement_id) { sizes.total = m_database.result_column<u64>(statement_id, 0); },
UnixDateTime::earliest());
return sizes;
}
}