Link the cache size to the book count.

Unless explicitly set via user env variable.
This commit is contained in:
Matthieu Gautier 2022-04-26 17:55:56 +02:00
parent 582e3ec46d
commit 740581c55c
3 changed files with 24 additions and 1 deletions

View File

@ -33,6 +33,7 @@
#include <pugixml.hpp> #include <pugixml.hpp>
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <cmath>
#include <unicode/locid.h> #include <unicode/locid.h>
#include <xapian.h> #include <xapian.h>
@ -151,6 +152,10 @@ bool Library::addBook(const Book& book)
auto& newEntry = mp_impl->m_books[book.getId()]; auto& newEntry = mp_impl->m_books[book.getId()];
static_cast<Book&>(newEntry) = book; static_cast<Book&>(newEntry) = book;
newEntry.lastUpdatedRevision = mp_impl->m_revision; newEntry.lastUpdatedRevision = mp_impl->m_revision;
size_t new_cache_size = std::ceil(mp_impl->getBookCount(true, true)*0.1);
if (getEnvVar<int>("ARCHIVE_CACHE_SIZE", -1) <= 0) {
mp_impl->mp_archiveCache->setMaxSize(new_cache_size);
}
return true; return true;
} }
} }
@ -184,6 +189,12 @@ bool Library::removeBookById(const std::string& id)
std::lock_guard<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
mp_impl->m_bookDB.delete_document("Q" + id); mp_impl->m_bookDB.delete_document("Q" + id);
dropCache(id); dropCache(id);
// We do not change the cache size here
// Most of the time, the book is remove in case of library refresh, it is
// often associated with addBook calls (which will properly set the cache size)
// Having a too big cache is not a problem here (or it would have been before)
// (And setMaxSize doesn't actually reduce the cache size, extra cached items
// will be removed in put or getOrPut).
return mp_impl->m_books.erase(id) == 1; return mp_impl->m_books.erase(id) == 1;
} }

View File

@ -1,3 +1,4 @@
/* /*
* Copyright (C) 2021 Matthieu Gautier <mgautier@kymeria.fr> * Copyright (C) 2021 Matthieu Gautier <mgautier@kymeria.fr>
* Copyright (C) 2020 Veloman Yunkan * Copyright (C) 2020 Veloman Yunkan
@ -84,6 +85,11 @@ public: // types
return impl_.drop(key); return impl_.drop(key);
} }
size_t setMaxSize(size_t new_size) {
std::unique_lock<std::mutex> l(lock_);
return impl_.setMaxSize(new_size);
}
private: // data private: // data
Impl impl_; Impl impl_;
std::mutex lock_; std::mutex lock_;

View File

@ -138,12 +138,18 @@ public: // functions
return _cache_items_map.size(); return _cache_items_map.size();
} }
size_t setMaxSize(size_t new_size) {
size_t previous = _max_size;
_max_size = new_size;
return previous;
}
private: // functions private: // functions
void putMissing(const key_t& key, const value_t& value) { void putMissing(const key_t& key, const value_t& value) {
assert(_cache_items_map.find(key) == _cache_items_map.end()); assert(_cache_items_map.find(key) == _cache_items_map.end());
_cache_items_list.push_front(key_value_pair_t(key, value)); _cache_items_list.push_front(key_value_pair_t(key, value));
_cache_items_map[key] = _cache_items_list.begin(); _cache_items_map[key] = _cache_items_list.begin();
if (_cache_items_map.size() > _max_size) { while (_cache_items_map.size() > _max_size) {
_cache_items_map.erase(_cache_items_list.back().first); _cache_items_map.erase(_cache_items_list.back().first);
_cache_items_list.pop_back(); _cache_items_list.pop_back();
} }