diff --git a/include/core_api.h b/include/core_api.h index f435ccc4..a549c4a1 100644 --- a/include/core_api.h +++ b/include/core_api.h @@ -155,3 +155,5 @@ bool is_doc_write_route(uint64_t route_hash); bool is_doc_del_route(uint64_t route_hash); Option> get_api_key_and_ip(const std::string& metadata); + +void init_api(uint32_t cache_num_entries); diff --git a/include/tsconfig.h b/include/tsconfig.h index 57796930..50f5e094 100644 --- a/include/tsconfig.h +++ b/include/tsconfig.h @@ -58,6 +58,8 @@ private: int disk_used_max_percentage; int memory_used_max_percentage; + uint32_t cache_num_entries = 1000; + std::atomic skip_writes; std::atomic 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("num-documents-parallel-load"); } + if(options.exist("cache-num-entries")) { + this->cache_num_entries = options.get("cache-num-entries"); + } + if(options.exist("thread-pool-size")) { this->thread_pool_size = options.get("thread-pool-size"); } diff --git a/src/core_api.cpp b/src/core_api.cpp index 6732d0aa..b32176d7 100644 --- a/src/core_api.cpp +++ b/src/core_api.cpp @@ -19,6 +19,10 @@ using namespace std::chrono_literals; std::shared_mutex mutex; LRU::Cache res_cache; +void init_api(uint32_t cache_num_entries) { + res_cache.capacity(cache_num_entries); +} + bool handle_authentication(std::map& req_params, std::vector& embedded_params_vec, const std::string& body, @@ -355,7 +359,8 @@ bool get_search(const std::shared_ptr& req, const std::shared_ptr& 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); } } diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp index b841c515..6dad2175 100644 --- a/src/main/typesense_server.cpp +++ b/src/main/typesense_server.cpp @@ -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); } \ No newline at end of file