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

Commit b8a6b60

Browse files
author
Yu Wei Wu
committed
feat(utils): Refactor cache interface
Refactor cache interface to match the need of configuration.
1 parent 0acae98 commit b8a6b60

File tree

4 files changed

+40
-55
lines changed

4 files changed

+40
-55
lines changed

accelerator/common_core.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req,
113113
flex_trit_t* elt = NULL;
114114
char cache_key[NUM_TRYTES_HASH] = {0};
115115
char cache_value[NUM_TRYTES_SERIALIZED_TRANSACTION] = {0};
116-
cache_t* cache = cache_init();
116+
cache_init();
117117

118118
// create bundle
119119
bundle_transactions_new(&bundle);
@@ -127,7 +127,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req,
127127
flex_trits_to_trytes(
128128
(tryte_t*)cache_value, NUM_TRYTES_SERIALIZED_TRANSACTION, elt,
129129
NUM_TRITS_SERIALIZED_TRANSACTION, NUM_TRITS_SERIALIZED_TRANSACTION);
130-
ret = cache_set(cache, cache_key, cache_value);
130+
ret = cache_set(cache_key, cache_value);
131131
if (ret) {
132132
goto done;
133133
}
@@ -154,7 +154,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req,
154154
}
155155

156156
done:
157-
cache_stop(&cache);
157+
cache_stop();
158158
bundle_transactions_free(&bundle);
159159
return ret;
160160
}
@@ -355,16 +355,16 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service,
355355
char cache_value[FLEX_TRIT_SIZE_8019] = {0};
356356

357357
// get raw transaction data of transaction hashes
358-
cache_t* cache = cache_init();
358+
cache_init();
359359
get_trytes_req_t* get_trytes_req = get_trytes_req_new();
360360
get_trytes_res_t* get_trytes_res = get_trytes_res_new();
361-
if (cache == NULL || get_trytes_req == NULL || get_trytes_res == NULL) {
361+
if (get_trytes_req == NULL || get_trytes_res == NULL) {
362362
ret = SC_CCLIENT_OOM;
363363
goto done;
364364
}
365365

366366
// get in cache first, then get from full node if no result in cache
367-
ret = cache_get(cache, req, cache_value);
367+
ret = cache_get(req, cache_value);
368368
if (ret == SC_OK) {
369369
flex_trits_from_trytes(
370370
tx_trits, NUM_TRITS_SERIALIZED_TRANSACTION, (const tryte_t*)cache_value,
@@ -390,7 +390,7 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service,
390390
flex_trits_to_trytes(
391391
(tryte_t*)cache_value, NUM_TRYTES_SERIALIZED_TRANSACTION, tx_trits,
392392
NUM_TRITS_SERIALIZED_TRANSACTION, NUM_TRITS_SERIALIZED_TRANSACTION);
393-
cache_set(cache, req, cache_value);
393+
cache_set(req, cache_value);
394394
} else {
395395
ret = SC_CCLIENT_NOT_FOUND;
396396
goto done;
@@ -406,7 +406,7 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service,
406406
transaction_set_hash(res->txn, hash_trits);
407407

408408
done:
409-
cache_stop(&cache);
409+
cache_stop();
410410
get_trytes_req_free(&get_trytes_req);
411411
get_trytes_res_free(&get_trytes_res);
412412
return ret;

tests/test_cache.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,30 @@
22
#include "utils/cache.h"
33

44
void test_cache_del(void) {
5-
cache_t* cache = cache_init();
65
const char* key = TRYTES_81_1;
7-
cache_del(cache, key);
8-
cache_stop(&cache);
6+
cache_del(key);
97
}
108

119
void test_cache_get(void) {
12-
cache_t* cache = cache_init();
1310
const char* key = TRYTES_81_1;
1411
char res[TRYTES_2673_LEN + 1] = {0};
15-
cache_get(cache, key, res);
12+
cache_get(key, res);
1613
res[TRYTES_2673_LEN] = '\0';
1714
TEST_ASSERT_EQUAL_STRING(res, TRYTES_2673_1);
18-
cache_stop(&cache);
1915
}
2016

2117
void test_cache_set(void) {
22-
cache_t* cache = cache_init();
2318
const char* key = TRYTES_81_1;
2419
const char* value = TRYTES_2673_1;
25-
cache_set(cache, key, value);
26-
cache_stop(&cache);
20+
cache_set(key, value);
2721
}
2822

2923
int main(void) {
3024
UNITY_BEGIN();
31-
25+
cache_init();
3226
RUN_TEST(test_cache_set);
3327
RUN_TEST(test_cache_get);
3428
RUN_TEST(test_cache_del);
29+
cache_stop();
3530
return UNITY_END();
3631
}

utils/backend_redis.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
typedef struct {
77
redisContext* rc;
88
} connection_private;
9-
#define CONN(c) ((connection_private*)(c->conn))
9+
#define CONN(c) ((connection_private*)(c.conn))
10+
11+
static cache_t cache;
1012

1113
/*
1214
* Private functions
@@ -68,39 +70,33 @@ static status_t redis_set(redisContext* c, const char* const key,
6870
* Public functions
6971
*/
7072

71-
cache_t* cache_init() {
72-
cache_t* cache = (cache_t*)malloc(sizeof(cache_t));
73-
if (cache != NULL) {
74-
cache->conn = (connection_private*)malloc(sizeof(connection_private));
75-
CONN(cache)->rc = redisConnect(REDIS_HOST, REDIS_PORT);
76-
return cache;
73+
bool cache_init() {
74+
cache.conn = (connection_private*)malloc(sizeof(connection_private));
75+
CONN(cache)->rc = redisConnect(REDIS_HOST, REDIS_PORT);
76+
if (CONN(cache)->rc) {
77+
return true;
7778
}
78-
return NULL;
79+
return false;
7980
}
8081

81-
void cache_stop(cache_t** cache) {
82-
if (*cache) {
83-
redisFree(CONN((*cache))->rc);
82+
void cache_stop() {
83+
if (CONN(cache)->rc) {
84+
redisFree(CONN(cache)->rc);
8485

85-
if (CONN((*cache))) {
86-
free(CONN((*cache)));
86+
if (CONN(cache)) {
87+
free(CONN(cache));
8788
}
88-
89-
free(*cache);
90-
*cache = NULL;
9189
}
9290
}
9391

94-
status_t cache_del(const cache_t* const cache, const char* const key) {
92+
status_t cache_del(const char* const key) {
9593
return redis_del(CONN(cache)->rc, key);
9694
}
9795

98-
status_t cache_get(const cache_t* const cache, const char* const key,
99-
char* res) {
96+
status_t cache_get(const char* const key, char* res) {
10097
return redis_get(CONN(cache)->rc, key, res);
10198
}
10299

103-
status_t cache_set(const cache_t* const cache, const char* const key,
104-
const char* const value) {
100+
status_t cache_set(const char* const key, const char* const value) {
105101
return redis_set(CONN(cache)->rc, key, value);
106102
}

utils/cache.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,40 @@ typedef struct {
2525
* Initiate cache module
2626
*
2727
* @return
28-
* - struct of cache_t on success
29-
* - NULL on error
28+
* - True on success
29+
* - False on error
3030
*/
31-
cache_t* cache_init();
31+
bool cache_init();
3232

3333
/**
3434
* Stop interacting with cache module
3535
*
36-
* @param cache Data type for Cache module
37-
*
3836
* @return NULL
3937
*/
40-
void cache_stop(cache_t** cache);
38+
void cache_stop();
4139

4240
/**
4341
* Delete certain key-value store from cache
4442
*
45-
* @param[in] cache Data type for Cache module
4643
* @param[in] key Key string to search
4744
*
4845
* @return
4946
* - SC_OK on success
50-
* - 1 on error
47+
* - non-zero on error
5148
*/
52-
status_t cache_del(const cache_t* const cache, const char* const key);
49+
status_t cache_del(const char* const key);
5350

5451
/**
5552
* Get key-value store from in-memory cache
5653
*
57-
* @param[in] cache Data type for Cache module
5854
* @param[in] key Key string to search
5955
* @param[out] res Result of GET key
6056
*
6157
* @return
6258
* - SC_OK on success
63-
* - 1 on error
59+
* - non-zero on error
6460
*/
65-
status_t cache_get(const cache_t* const cache, const char* const key,
66-
char* res);
61+
status_t cache_get(const char* const key, char* res);
6762

6863
/**
6964
* Set key-value store into in-memory cache
@@ -74,10 +69,9 @@ status_t cache_get(const cache_t* const cache, const char* const key,
7469
*
7570
* @return
7671
* - SC_OK on success
77-
* - 1 on error
72+
* - non-zero on error
7873
*/
79-
status_t cache_set(const cache_t* const cache, const char* const key,
80-
const char* const value);
74+
status_t cache_set(const char* const key, const char* const value);
8175

8276
#ifdef __cplusplus
8377
}

0 commit comments

Comments
 (0)