Parameterize cache size.

This commit is contained in:
Kishore Nallan 2023-05-15 14:50:50 +05:30
parent 72c5595780
commit 7c158e6828
4 changed files with 32 additions and 1 deletions

View File

@ -155,3 +155,5 @@ bool is_doc_write_route(uint64_t route_hash);
bool is_doc_del_route(uint64_t route_hash);
Option<std::pair<std::string,std::string>> get_api_key_and_ip(const std::string& metadata);
void init_api(uint32_t cache_num_entries);

View File

@ -58,6 +58,8 @@ private:
int disk_used_max_percentage;
int memory_used_max_percentage;
uint32_t cache_num_entries = 1000;
std::atomic<bool> skip_writes;
std::atomic<int> log_slow_searches_time_ms;
@ -79,6 +81,7 @@ protected:
this->log_slow_requests_time_ms = -1;
this->num_collections_parallel_load = 0; // will be set dynamically if not overridden
this->num_documents_parallel_load = 1000;
this->cache_num_entries = 1000;
this->thread_pool_size = 0; // will be set dynamically if not overridden
this->ssl_refresh_interval_seconds = 8 * 60 * 60;
this->enable_access_logging = false;
@ -284,6 +287,10 @@ public:
return this->num_documents_parallel_load;
}
size_t get_cache_num_entries() const {
return this->cache_num_entries;
}
size_t get_thread_pool_size() const {
return this->thread_pool_size;
}
@ -407,6 +414,10 @@ public:
this->num_documents_parallel_load = std::stoi(get_env("TYPESENSE_NUM_DOCUMENTS_PARALLEL_LOAD"));
}
if(!get_env("TYPESENSE_CACHE_NUM_ENTRIES").empty()) {
this->cache_num_entries = std::stoi(get_env("TYPESENSE_CACHE_NUM_ENTRIES"));
}
if(!get_env("TYPESENSE_THREAD_POOL_SIZE").empty()) {
this->thread_pool_size = std::stoi(get_env("TYPESENSE_THREAD_POOL_SIZE"));
}
@ -561,6 +572,10 @@ public:
this->num_documents_parallel_load = (int) reader.GetInteger("server", "num-documents-parallel-load", 1000);
}
if(reader.Exists("server", "cache-num-entries")) {
this->cache_num_entries = (int) reader.GetInteger("server", "cache-num-entries", 1000);
}
if(reader.Exists("server", "thread-pool-size")) {
this->thread_pool_size = (int) reader.GetInteger("server", "thread-pool-size", 0);
}
@ -709,6 +724,10 @@ public:
this->num_documents_parallel_load = options.get<uint32_t>("num-documents-parallel-load");
}
if(options.exist("cache-num-entries")) {
this->cache_num_entries = options.get<uint32_t>("cache-num-entries");
}
if(options.exist("thread-pool-size")) {
this->thread_pool_size = options.get<uint32_t>("thread-pool-size");
}

View File

@ -19,6 +19,10 @@ using namespace std::chrono_literals;
std::shared_mutex mutex;
LRU::Cache<uint64_t, cached_res_t> res_cache;
void init_api(uint32_t cache_num_entries) {
res_cache.capacity(cache_num_entries);
}
bool handle_authentication(std::map<std::string, std::string>& req_params,
std::vector<nlohmann::json>& embedded_params_vec,
const std::string& body,
@ -355,7 +359,8 @@ bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
return true;
}
//LOG(INFO) << "Result found in cache but ttl lapsed.";
// Result found in cache but ttl has lapsed.
res_cache.erase(req_hash);
}
}
@ -447,6 +452,9 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
res->set_content(cached_value.status_code, cached_value.content_type_header, cached_value.body, true);
return true;
}
// Result found in cache but ttl has lapsed.
res_cache.erase(req_hash);
}
}

View File

@ -169,5 +169,7 @@ int main(int argc, char **argv) {
signal(SIGINT, catch_interrupt);
signal(SIGTERM, catch_interrupt);
init_api(config.get_cache_num_entries());
return run_server(config, TYPESENSE_VERSION, &master_server_routes);
}