Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(config): Move cache init/destroy to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu Wei Wu committed Mar 26, 2019
1 parent b8a6b60 commit 0cfb0ac
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ cc_library(
":ta_errors",
"//request",
"//response",
"//utils:cache",
"@com_github_uthash//:uthash",
"@entangled//common/model:bundle",
"@entangled//utils:time",
Expand All @@ -57,6 +56,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":ta_errors",
"//utils:cache",
"//utils:pow",
"@entangled//cclient/api",
"@entangled//cclient/types",
Expand Down
5 changes: 0 additions & 5 deletions accelerator/common_core.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "common_core.h"
#include <sys/time.h>
#include "utils/cache.h"

status_t cclient_get_txn_to_approve(const iota_client_service_t* const service,
uint8_t const depth,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
1 change: 1 addition & 0 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ cc_library(
name = "test_define",
hdrs = ["test_define.h"],
deps = [
"@entangled//cclient/types",
"//accelerator:ta_config",
"@unity",
],
)
2 changes: 1 addition & 1 deletion tests/test_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/test_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define TESTS_TEST_DEFINE_H_

#include <unity/unity.h>
#include "cclient/types/types.h"
#include "accelerator/config.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 0 additions & 1 deletion utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions utils/backend_redis.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "accelerator/config.h"
#include "cache.h"
#include "third_party/hiredis/hiredis.h"

Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 8 additions & 1 deletion utils/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0cfb0ac

Please sign in to comment.