diff --git a/CMakeLists.txt b/CMakeLists.txt index 622819b36d5f32df5a568b97580b04c785625a1f..be5ac24b64520fcd0f10c0694af6ea1f26ba22e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ file(GLOB CORE_SERVER_HEADERS *.h) add_library(${PROJECT_NAME} STATIC ${CORE_SERVER_SOURCES} ${CORE_SERVER_HEADERS}) -target_link_libraries(${PROJECT_NAME} pthread ev dap_crypto) +target_link_libraries(${PROJECT_NAME} pthread ev memcached dap_crypto) target_include_directories(${PROJECT_NAME} INTERFACE .) set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) diff --git a/dap_memcached.c b/dap_memcached.c new file mode 100644 index 0000000000000000000000000000000000000000..4ce64a2b92b904d90db79fc5c0c601a2ea13f4a4 --- /dev/null +++ b/dap_memcached.c @@ -0,0 +1,68 @@ +#include "dap_memcached.h" +#include <libmemcached/memcached.h> + +static memcached_st *_memc; +static time_t _expiration; +static bool _is_module_enable = false; + +int dap_memcached_init(const char *server_host, uint16_t port, time_t expiration) +{ + _expiration = expiration; + memcached_return rc; + memcached_server_st *servers = NULL; + + char *test_key = "test_key"; + char *test_value = "test_value"; + + _memc = memcached_create(NULL); + + servers= memcached_server_list_append(servers, server_host, port, &rc); + rc= memcached_server_push(_memc, servers); + + if (rc != MEMCACHED_SUCCESS) { + log_it(L_ERROR, "Couldn't add server: %s", memcached_strerror(_memc, rc)); + return -1; + } + + if(dap_memcache_put(test_key, test_value, strlen(test_value)) != true) { + return -2; + } + + if(_expiration == 0) { + log_it(L_WARNING, "Init memcached module without expiration value"); + } + + _is_module_enable = true; + return 0; +} + +bool dap_memcache_is_enable() +{ + return _is_module_enable; +} + +bool dap_memcache_put(const char* key, void *value, size_t value_size) +{ + memcached_return rc; + rc = memcached_set(_memc, key, strlen(key), value, value_size, _expiration, (uint32_t)0); + if (rc != MEMCACHED_SUCCESS) { + log_it(L_ERROR, "%s", memcached_strerror(_memc, rc)); + return false; + } + return true; +} + +bool dap_memcache_get(const char* key, size_t * value_size, void ** result) +{ + memcached_return rc; + *result = memcached_get(_memc, key, strlen(key), value_size, NULL, &rc); + return rc == MEMCACHED_SUCCESS; +} + +/** + * @brief dap_memcached_deinit + */ +void dap_memcached_deinit() +{ + _is_module_enable = false; +} diff --git a/dap_memcached.h b/dap_memcached.h new file mode 100644 index 0000000000000000000000000000000000000000..4881236a873db1ffe8595e188b8374c283155d60 --- /dev/null +++ b/dap_memcached.h @@ -0,0 +1,45 @@ +#pragma once + +#undef LOG_TAG +#define LOG_TAG "dap_memcached" + +#include <stdint.h> +#include <stdbool.h> + +#include "dap_common.h" + +/** + * @brief dap_memcached_init + * @param server_host + * @param port + * @param expiration key value in mamcached store. If zero - expiration disable + * @return + */ +int dap_memcached_init(const char *server_host, uint16_t port, time_t expiration); + +/** + * @brief is_dap_memcache_enable + * @return + */ +bool dap_memcache_is_enable(void); + +/** + * @brief dap_memcached_deinit + */ +void dap_memcached_deinit(void); + +/** + * @brief dap_memcache_put + * @param key + * @param value + * @param value_size + * @return + */ +bool dap_memcache_put(const char* key, void *value, size_t value_size); + +/** + * @brief dap_memcache_get + * @param key + * @return true if key found + */ +bool dap_memcache_get(const char* key, size_t * value_size, void ** result); diff --git a/libdap b/libdap index c30973bfe5a3d2eb4201af535cc559b326e8b175..0255887bbca085979fcd26e70a8b5131a428c36d 160000 --- a/libdap +++ b/libdap @@ -1 +1 @@ -Subproject commit c30973bfe5a3d2eb4201af535cc559b326e8b175 +Subproject commit 0255887bbca085979fcd26e70a8b5131a428c36d