Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Add optional LRU + TTL cache for searches.

Kishore Nallan9679895ee388
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 13171754..ae7312bc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -58,6 +58,8 @@ include(cmake/TestResources.cmake)
include(cmake/Iconv.cmake)
include(cmake/Jemalloc.cmake)
include(cmake/h3.cmake)
+include(cmake/s2.cmake)
+include(cmake/lrucache.cmake)
include(cmake/kakasi.cmake)
FIND_PACKAGE(OpenSSL 1.1.1 REQUIRED)
@@ -89,6 +91,8 @@ include_directories(${DEP_ROOT_DIR}/${BRPC_NAME}/include)
include_directories(${DEP_ROOT_DIR}/${BRAFT_NAME}/include)
include_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/include/jemalloc)
include_directories(${DEP_ROOT_DIR}/${H3_NAME}/build/src/h3lib/include)
+include_directories(${DEP_ROOT_DIR}/${S2_NAME}/src)
+include_directories(${DEP_ROOT_DIR}/${LRUCACHE_NAME}/include)
include_directories(${DEP_ROOT_DIR}/${KAKASI_NAME}/build/include)
include_directories(${DEP_ROOT_DIR}/${KAKASI_NAME}/data)
@@ -100,6 +104,7 @@ link_directories(${DEP_ROOT_DIR}/${ROCKSDB_NAME})
link_directories(${DEP_ROOT_DIR}/${ICONV_NAME}/lib/.libs)
link_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/lib)
link_directories(${DEP_ROOT_DIR}/${H3_NAME}/build/lib)
+link_directories(${DEP_ROOT_DIR}/${S2_NAME}/build)
link_directories(${DEP_ROOT_DIR}/${KAKASI_NAME}/build/lib)
set(JEMALLOC_ROOT_DIR "${DEP_ROOT_DIR}/${JEMALLOC_NAME}")
diff --git a/cmake/lrucache.cmake b/cmake/lrucache.cmake
new file mode 100644
index 00000000..bfc666d0
--- /dev/null
+++ b/cmake/lrucache.cmake
@@ -0,0 +1,15 @@
+# Download and build LRUCACHE
+
+set(LRUCACHE_VERSION 13f30ad33a227a3e9682578c450777380ecddfcf)
+set(LRUCACHE_NAME lru-cache-${LRUCACHE_VERSION})
+set(LRUCACHE_TAR_PATH ${DEP_ROOT_DIR}/${LRUCACHE_NAME}.tar.gz)
+
+if(NOT EXISTS ${LRUCACHE_TAR_PATH})
+ message(STATUS "Downloading https://github.com/goldsborough/lru-cache/archive/${LRUCACHE_VERSION}.tar.gz")
+ file(DOWNLOAD https://github.com/goldsborough/lru-cache/archive/${LRUCACHE_VERSION}.tar.gz ${LRUCACHE_TAR_PATH})
+endif()
+
+if(NOT EXISTS ${DEP_ROOT_DIR}/${LRUCACHE_NAME})
+ message(STATUS "Extracting ${LRUCACHE_NAME}...")
+ execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${LRUCACHE_TAR_PATH} WORKING_DIRECTORY ${DEP_ROOT_DIR}/)
+endif()
diff --git a/cmake/s2.cmake b/cmake/s2.cmake
new file mode 100644
index 00000000..009498a2
--- /dev/null
+++ b/cmake/s2.cmake
@@ -0,0 +1,45 @@
+# Download and build S2
+
+set(S2_VERSION 0.9.0)
+set(S2_NAME s2geometry-${S2_VERSION})
+set(S2_TAR_PATH ${DEP_ROOT_DIR}/${S2_NAME}.tar.gz)
+
+if(NOT EXISTS ${S2_TAR_PATH})
+ message(STATUS "Downloading https://github.com/google/s2geometry/archive/v${S2_VERSION}.tar.gz")
+ file(DOWNLOAD https://github.com/google/s2geometry/archive/v${S2_VERSION}.tar.gz ${S2_TAR_PATH})
+endif()
+
+if(NOT EXISTS ${DEP_ROOT_DIR}/${S2_NAME})
+ message(STATUS "Extracting ${S2_NAME}...")
+ execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${S2_TAR_PATH} WORKING_DIRECTORY ${DEP_ROOT_DIR}/)
+endif()
+
+if(NOT EXISTS ${DEP_ROOT_DIR}/${S2_NAME}/build/libs2.a)
+ message("Configuring ${S2_NAME}...")
+
+ file(REMOVE_RECURSE ${DEP_ROOT_DIR}/${S2_NAME}/build)
+ file(MAKE_DIRECTORY ${DEP_ROOT_DIR}/${S2_NAME}/build)
+
+ execute_process(COMMAND ${CMAKE_COMMAND}
+ "-DCMAKE_FIND_LIBRARY_SUFFIXES=.a"
+ "-DBUILD_SHARED_LIBS=OFF"
+ "-H${DEP_ROOT_DIR}/${S2_NAME}"
+ "-B${DEP_ROOT_DIR}/${S2_NAME}/build"
+ RESULT_VARIABLE
+ S2_CONFIGURE)
+ if(NOT S2_CONFIGURE EQUAL 0)
+ message(FATAL_ERROR "${S2_NAME} configure failed!")
+ endif()
+
+ if(BUILD_DEPS STREQUAL "yes")
+ message("Building ${S2_NAME} locally...")
+ execute_process(COMMAND ${CMAKE_COMMAND} --build
+ "${DEP_ROOT_DIR}/${S2_NAME}/build"
+ --target s2
+ RESULT_VARIABLE
+ S2_BUILD)
+ if(NOT S2_BUILD EQUAL 0)
+ message(FATAL_ERROR "${S2_NAME} build failed!")
+ endif()
+ endif()
+endif()
diff --git a/include/http_data.h b/include/http_data.h
index 7eda382e..a8219a05 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -83,6 +83,12 @@ struct http_res {
//LOG(INFO) << "http_res " << this;
}
+ void load(uint32_t status_code, const std::string& content_type_header, const std::string& body) {
+ this->status_code = status_code;
+ this->content_type_header = content_type_header;
+ this->body = body;
+ }
+
void wait() {
auto lk = std::unique_lock<std::mutex>(mcv);
cv.wait(lk, [&] { return ready; });
@@ -181,6 +187,28 @@ struct http_res {
}
};
+struct cached_res_t {
+ uint32_t status_code;
+ std::string content_type_header;
+ std::string body;
+ uint64_t hash;
+
+ bool operator == (const cached_res_t& res) const {
+ return hash == res.hash;
+ }
+
+ bool operator != (const cached_res_t& res) const {
+ return hash != res.hash;
+ }
+
+ void load(uint32_t status_code, const std::string& content_type_header, const std::string& body, uint64_t hash) {
+ this->status_code = status_code;
+ this->content_type_header = content_type_header;
+ this->body = body;
+ this->hash = hash;
+ }
+};
+
struct http_req {
static constexpr const char* AUTH_HEADER = "x-typesense-api-key";
diff --git a/src/core_api.cpp b/src/core_api.cpp
index cfc07a52..966aa4cc 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -10,6 +10,12 @@
#include "system_metrics.h"
#include "logger.h"
#include "core_api_utils.h"
+#include "lru/lru.hpp"
+
+using namespace std::chrono_literals;
+
+std::shared_mutex mutex;
+LRU::TimedCache<uint64_t, cached_res_t> res_cache(60*1000ms, 1000);
bool handle_authentication(std::map<std::string, std::string>& req_params, const std::string& body,
const route_path& rpath, const std::string& auth_key) {
@@ -190,7 +196,41 @@ bool get_log_sequence(const std::shared_ptr<http_req>& req, const std::shared_pt
return true;
}
+uint64_t hash_request(const std::shared_ptr<http_req>& req) {
+ std::stringstream ss;
+ ss << req->route_hash << req->body;
+
+ for(auto& kv: req->params) {
+ if(kv.first != "use_cache") {
+ ss << kv.second;
+ }
+ }
+
+ const std::string& req_str = ss.str();
+ return StringUtils::hash_wy(req_str.c_str(), req_str.size());
+}
+
bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ const auto cache_it = req->params.find("use_cache");
+ bool use_cache = (cache_it != req->params.end()) && (cache_it->second == "1" || cache_it->second == "true");
+ uint64_t req_hash = 0;
+
+ if(use_cache) {
+ // cache enabled, let's check if request is already in the cache
+ req_hash = hash_request(req);
+
+ //LOG(INFO) << "req_hash = " << req_hash;
+
+ std::shared_lock lock(mutex);
+ auto hit_it = res_cache.find(req_hash);
+ if(hit_it != res_cache.end()) {
+ //LOG(INFO) << "Result found in cache.";
+ const auto& cached_value = hit_it.value();
+ res->load(cached_value.status_code, cached_value.content_type_header, cached_value.body);
+ return true;
+ }
+ }
+
std::string results_json_str;
Option<bool> search_op = CollectionManager::do_search(req->params, results_json_str);
@@ -201,10 +241,42 @@ bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
res->set_200(results_json_str);
+ // we will cache only successful requests
+ if(use_cache) {
+ //LOG(INFO) << "Adding to cache, key = " << req_hash;
+ cached_res_t cached_res;
+ cached_res.load(res->status_code, res->content_type_header, res->body, req_hash);
+ std::unique_lock lock(mutex);
+
+ // NOTE: due to an implementation quirk, erase is required for dealing with expired keys that might still exist
+ res_cache.erase(req_hash);
+ res_cache.insert(req_hash, cached_res);
+ }
+
return true;
}
bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+ const auto cache_it = req->params.find("use_cache");
+ bool use_cache = (cache_it != req->params.end()) && (cache_it->second == "1" || cache_it->second == "true");
+ uint64_t req_hash = 0;
+
+ if(use_cache) {
+ // cache enabled, let's check if request is already in the cache
+ req_hash = hash_request(req);
+
+ //LOG(INFO) << "req_hash = " << req_hash;
+
+ std::shared_lock lock(mutex);
+ auto hit_it = res_cache.find(req_hash);
+ if(hit_it != res_cache.end()) {
+ //LOG(INFO) << "Result found in cache.";
+ const auto& cached_value = hit_it.value();
+ res->load(cached_value.status_code, cached_value.content_type_header, cached_value.body);
+ return true;
+ }
+ }
+
nlohmann::json req_json;
try {
@@ -275,6 +347,19 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
}
res->set_200(response.dump());
+
+ // we will cache only successful requests
+ if(use_cache) {
+ //LOG(INFO) << "Adding to cache, key = " << req_hash;
+ cached_res_t cached_res;
+ cached_res.load(res->status_code, res->content_type_header, res->body, req_hash);
+ std::unique_lock lock(mutex);
+
+ // NOTE: due to an implementation quirk, erase is required for dealing with expired keys that might still exist
+ res_cache.erase(req_hash);
+ res_cache.insert(req_hash, cached_res);
+ }
+
return true;
}