Issue #10: Remove too old pages from cache

This commit is contained in:
Jakub Melka
2022-02-05 18:18:21 +01:00
parent 49cab7937a
commit 8bf2913ded
5 changed files with 106 additions and 1 deletions

View File

@ -217,7 +217,8 @@ const PDFPrecompiledPage* PDFAsynchronousPageCompiler::getCompiledPage(PDFIntege
return nullptr;
}
const PDFPrecompiledPage* page = m_cache.object(pageIndex);
PDFPrecompiledPage* page = m_cache.object(pageIndex);
if (!page && compile)
{
QMutexLocker locker(&m_mutex);
@ -228,9 +229,43 @@ const PDFPrecompiledPage* PDFAsynchronousPageCompiler::getCompiledPage(PDFIntege
}
}
if (page)
{
page->markAccessed();
}
return page;
}
void PDFAsynchronousPageCompiler::smartClearCache(const int milisecondsLimit, const std::vector<PDFInteger>& activePages)
{
if (m_state != State::Active)
{
// Jakub Melka: Cache clearing can be done only in active state
return;
}
QMutexLocker locker(&m_mutex);
Q_ASSERT(std::is_sorted(activePages.cbegin(), activePages.cend()));
QList<PDFInteger> pageIndices = m_cache.keys();
for (const PDFInteger pageIndex : pageIndices)
{
if (std::binary_search(activePages.cbegin(), activePages.cend(), pageIndex))
{
// We do not remove active page
continue;
}
const PDFPrecompiledPage* page = m_cache.object(pageIndex);
if (page && page->hasExpired(milisecondsLimit))
{
m_cache.remove(pageIndex);
}
}
}
void PDFAsynchronousPageCompiler::onPageCompiled()
{
std::vector<PDFInteger> compiledPages;
@ -249,6 +284,7 @@ void PDFAsynchronousPageCompiler::onPageCompiled()
{
// If we are in active state, try to store precompiled page
PDFPrecompiledPage* page = new PDFPrecompiledPage(std::move(task.precompiledPage));
page->markAccessed();
qint64 memoryConsumptionEstimate = page->getMemoryConsumptionEstimate();
if (m_cache.insert(it->first, page, memoryConsumptionEstimate))
{