From 0cfb0ac6a10903e54cbddc3daeee637fe0f3d074 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Tue, 26 Mar 2019 15:35:43 +0800 Subject: [PATCH] feat(config): Move cache init/destroy to config --- accelerator/BUILD | 2 +- accelerator/common_core.c | 5 ----- accelerator/config.c | 3 +++ accelerator/config.h | 1 + tests/BUILD | 2 +- tests/test_cache.c | 2 +- tests/test_common.cc | 2 ++ tests/test_define.h | 2 +- utils/BUILD | 1 - utils/backend_redis.c | 5 ++--- utils/cache.h | 9 ++++++++- 11 files changed, 20 insertions(+), 14 deletions(-) diff --git a/accelerator/BUILD b/accelerator/BUILD index 3af2503e..e959d048 100644 --- a/accelerator/BUILD +++ b/accelerator/BUILD @@ -43,7 +43,6 @@ cc_library( ":ta_errors", "//request", "//response", - "//utils:cache", "@com_github_uthash//:uthash", "@entangled//common/model:bundle", "@entangled//utils:time", @@ -57,6 +56,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":ta_errors", + "//utils:cache", "//utils:pow", "@entangled//cclient/api", "@entangled//cclient/types", diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 6a9f8b00..2b7c7efa 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -1,6 +1,5 @@ #include "common_core.h" #include -#include "utils/cache.h" status_t cclient_get_txn_to_approve(const iota_client_service_t* const service, uint8_t const depth, @@ -113,7 +112,6 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, flex_trit_t* elt = NULL; char cache_key[NUM_TRYTES_HASH] = {0}; char cache_value[NUM_TRYTES_SERIALIZED_TRANSACTION] = {0}; - cache_init(); // create bundle bundle_transactions_new(&bundle); @@ -154,7 +152,6 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, } done: - cache_stop(); bundle_transactions_free(&bundle); return ret; } @@ -355,7 +352,6 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, char cache_value[FLEX_TRIT_SIZE_8019] = {0}; // get raw transaction data of transaction hashes - cache_init(); get_trytes_req_t* get_trytes_req = get_trytes_req_new(); get_trytes_res_t* get_trytes_res = get_trytes_res_new(); if (get_trytes_req == NULL || get_trytes_res == NULL) { @@ -406,7 +402,6 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, transaction_set_hash(res->txn, hash_trits); done: - cache_stop(); get_trytes_req_free(&get_trytes_req); get_trytes_res_free(&get_trytes_res); return ret; diff --git a/accelerator/config.c b/accelerator/config.c index daac92a1..c22ac709 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -43,6 +43,8 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle, pow_init(); + cache_init(REDIS_HOST, REDIS_PORT); + return ret; } @@ -52,5 +54,6 @@ void ta_config_destroy(iota_client_service_t* const service) { iota_client_core_destroy(service); pow_destroy(); + cache_stop(); logger_helper_release(logger_id); } diff --git a/accelerator/config.h b/accelerator/config.h index a61c8adc..0348f7d9 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -5,6 +5,7 @@ #include "cclient/api/core/core_api.h" #include "cclient/api/extended/extended_api.h" #include "cclient/types/types.h" +#include "utils/cache.h" #include "utils/pow.h" #ifdef __cplusplus diff --git a/tests/BUILD b/tests/BUILD index abc6e349..95c38883 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -86,7 +86,7 @@ cc_library( name = "test_define", hdrs = ["test_define.h"], deps = [ - "@entangled//cclient/types", + "//accelerator:ta_config", "@unity", ], ) diff --git a/tests/test_cache.c b/tests/test_cache.c index a54f6399..daaf6c88 100644 --- a/tests/test_cache.c +++ b/tests/test_cache.c @@ -22,7 +22,7 @@ void test_cache_set(void) { int main(void) { UNITY_BEGIN(); - cache_init(); + cache_init(REDIS_HOST, REDIS_PORT); RUN_TEST(test_cache_set); RUN_TEST(test_cache_get); RUN_TEST(test_cache_del); diff --git a/tests/test_common.cc b/tests/test_common.cc index 68d7de27..b5e44f3c 100644 --- a/tests/test_common.cc +++ b/tests/test_common.cc @@ -194,6 +194,8 @@ TEST(GetBundleTest, RetreiveBundleTest) { } int main(int argc, char** argv) { + // GTest manage to cleanup after testing, so only need to initialize here + cache_init(REDIS_HOST, REDIS_PORT); ::testing::GTEST_FLAG(throw_on_failure) = true; ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); diff --git a/tests/test_define.h b/tests/test_define.h index 86674d14..2a10db2e 100644 --- a/tests/test_define.h +++ b/tests/test_define.h @@ -2,7 +2,7 @@ #define TESTS_TEST_DEFINE_H_ #include -#include "cclient/types/types.h" +#include "accelerator/config.h" #ifdef __cplusplus extern "C" { diff --git a/utils/BUILD b/utils/BUILD index de8bf999..eac468e1 100644 --- a/utils/BUILD +++ b/utils/BUILD @@ -5,7 +5,6 @@ cc_library( srcs = ["backend_redis.c"], hdrs = ["cache.h"], deps = [ - "//accelerator:ta_config", "//accelerator:ta_errors", "//third_party:hiredis", "@entangled//cclient/types", diff --git a/utils/backend_redis.c b/utils/backend_redis.c index c3fc9c02..49acce38 100644 --- a/utils/backend_redis.c +++ b/utils/backend_redis.c @@ -1,4 +1,3 @@ -#include "accelerator/config.h" #include "cache.h" #include "third_party/hiredis/hiredis.h" @@ -70,9 +69,9 @@ static status_t redis_set(redisContext* c, const char* const key, * Public functions */ -bool cache_init() { +bool cache_init(const char* host, int port) { cache.conn = (connection_private*)malloc(sizeof(connection_private)); - CONN(cache)->rc = redisConnect(REDIS_HOST, REDIS_PORT); + CONN(cache)->rc = redisConnect(host, port); if (CONN(cache)->rc) { return true; } diff --git a/utils/cache.h b/utils/cache.h index f0ba9253..52744c30 100644 --- a/utils/cache.h +++ b/utils/cache.h @@ -8,6 +8,10 @@ #include "accelerator/errors.h" #include "cclient/types/types.h" +#ifdef __cplusplus +extern "C" { +#endif + /** * @file cache.h * @brief Implementation of cache interface @@ -24,11 +28,14 @@ typedef struct { /** * Initiate cache module * + * @param[in] host cache server host + * @param[in] port cache server port + * * @return * - True on success * - False on error */ -bool cache_init(); +bool cache_init(const char* host, int port); /** * Stop interacting with cache module