From e642d41c69041382609f43bb64882dc19c03be17 Mon Sep 17 00:00:00 2001 From: Maneesh P M Date: Tue, 12 Oct 2021 13:45:18 +0530 Subject: [PATCH] Retrieve Searcher and Search from LRU Cache --- src/server/internalServer.cpp | 36 +++++++++++++++++++++++++++-------- src/server/internalServer.h | 8 ++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 0e474ad86..c25f85b8f 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -57,8 +57,6 @@ extern "C" { #include #include -#include -#include #include #include @@ -79,6 +77,7 @@ extern "C" { #define MAX_SEARCH_LEN 140 #define KIWIX_MIN_CONTENT_SIZE_TO_DEFLATE 100 +#define SEARCH_CACHE_SIZE 2 namespace kiwix { @@ -139,7 +138,13 @@ InternalServer::InternalServer(Library* library, m_blockExternalLinks(blockExternalLinks), mp_daemon(nullptr), mp_library(library), - mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper) + mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper), + searcherCache((std::getenv("SEARCHER_CACHE_SIZE") != NULL) + ? atoi(std::getenv("SEARCHER_CACHE_SIZE")) + : std::max(1, (int) (mp_library->getBookCount(true, true)*0.1))), + searchCache((std::getenv("SEARCH_CACHE_SIZE")) + ? atoi(std::getenv("SEARCH_CACHE_SIZE")) + : SEARCH_CACHE_SIZE) {} bool InternalServer::start() { @@ -583,7 +588,12 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re std::shared_ptr searcher; if (archive) { - searcher = std::make_shared(*archive); + if (searcherCache.exist(bookId)) { + searcher = searcherCache.get(bookId); + } else { + searcher = std::make_shared(*archive); + searcherCache.put(bookId, searcher); + } } else { for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) { auto currentArchive = mp_library->getArchiveById(bookId); @@ -614,6 +624,7 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re } /* Get the results */ + std::string queryString; try { zim::Query query; if (patternString.empty()) { @@ -623,6 +634,7 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re } query.setQuery(""); + queryString = to_string(latitude) + to_string(longitude) + to_string(distance); query.setGeorange(latitude, longitude, distance); } else { // Execute Ft search @@ -630,13 +642,21 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re cout << "Performing query `" << patternString << "'" << endl; } - std::string queryString = removeAccents(patternString); + queryString = removeAccents(patternString); query.setQuery(queryString); } + queryString = bookId + queryString; + + std::shared_ptr search; + if (searchCache.exist(queryString)) { + search = searchCache.get(queryString); + } else { + search = make_shared(searcher->search(query)); + searchCache.put(queryString, search); + } - zim::Search search = searcher->search(query); - SearchRenderer renderer(search.getResults(start, pageLength), mp_nameMapper, start, - search.getEstimatedMatches()); + SearchRenderer renderer(search->getResults(start, pageLength), mp_nameMapper, start, + search->getEstimatedMatches()); renderer.setSearchPattern(patternString); renderer.setSearchContent(bookName); renderer.setProtocolPrefix(m_root + "/"); diff --git a/src/server/internalServer.h b/src/server/internalServer.h index cfaceb9ec..4b1510321 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -28,6 +28,9 @@ extern "C" { #include "library.h" #include "name_mapper.h" +#include +#include + #include #include @@ -36,6 +39,8 @@ extern "C" { #include "server/request_context.h" #include "server/response.h" +#include "tools/cache.cpp" + namespace kiwix { typedef kainjow::mustache::data MustacheData; @@ -108,6 +113,9 @@ class InternalServer { Library* mp_library; NameMapper* mp_nameMapper; + LRUCache> searcherCache; + LRUCache> searchCache; + std::string m_server_id; std::string m_library_id;