diff --git a/src/include/pgagroal.h b/src/include/pgagroal.h index 28fa5e89..04abd88f 100644 --- a/src/include/pgagroal.h +++ b/src/include/pgagroal.h @@ -447,8 +447,7 @@ struct query_cache struct hashTable { - struct hashEntry* key; - time_t valid_until; + char key[1024]; struct hashEntry* data; UT_hash_handle hh; } __attribute__ ((aligned (64))); diff --git a/src/include/query_cache.h b/src/include/query_cache.h index dcbb10a6..be6b1a92 100644 --- a/src/include/query_cache.h +++ b/src/include/query_cache.h @@ -81,7 +81,7 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem); * @warning The caller should ensure the validity of the 'Table' pointer and the 'key' pointer. */ struct hashEntry* -pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, struct hashEntry* key); +pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, char* key); /** * Invalidate a cache entry and remove it from the hash table. @@ -142,7 +142,7 @@ pgagroal_query_cache_update(struct hashTable** Table, struct hashEntry* key, str * The caller should manage memory to avoid leaks. */ int -pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, struct hashEntry* key, int flag); +pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, char* key); /** * Clear all cache entries from the hash table and free associated memory. diff --git a/src/libpgagroal/pipeline_session.c b/src/libpgagroal/pipeline_session.c index 37802858..99c4cdad 100644 --- a/src/libpgagroal/pipeline_session.c +++ b/src/libpgagroal/pipeline_session.c @@ -339,27 +339,13 @@ session_client(struct ev_loop* loop, struct ev_io* watcher, int revents) client_query->key_length = key_length; client_query->kind = msg->kind; + char key[1024]; + strcpy(key, msg->data + 5); + - struct hashEntry* key = NULL; - key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + client_query->key_length + 1); - - if (key == NULL) - { - - client_inactive(wi->slot); - ev_break(loop, EVBREAK_ONE); - return; - } - - memset(key->key, 0, client_query->key_length + 1); - - memcpy(key->key, client_query->key, key_length); - key->key[client_query->key_length + 1] = '\0'; - - key->length = client_query->key_length; struct hashEntry* s = pgagroal_query_cache_get(cache, &(cache->table), key); - if (s != NULL && s->value != NULL) + if (s != NULL) { // log cache hit struct message* result = NULL; @@ -552,60 +538,18 @@ session_server(struct ev_loop* loop, struct ev_io* watcher, int revents) if (!pgagroal_extract_message('Z', msg, &tmp)) { - struct hashEntry* key, * data; - key = (struct hashEntry*)malloc(sizeof(struct hashEntry) + client_query->key_length + 1); - data = (struct hashEntry*)malloc(sizeof(struct hashEntry) + msg->length); - - if (key == NULL || data == NULL) - { - - if (key != NULL) - { - free(key); - key = NULL; - } - if (data != NULL) - { - free(data); - data = NULL; - } - client_inactive(wi->slot); - ev_break(loop, EVBREAK_ONE); - - return; - } + char key[1024]; + struct hashEntry* data = NULL; + strcpy(key, client_query->key); + data = (struct hashEntry*)malloc(sizeof(struct hashEntry) + msg->length); data->value = malloc(msg->length); - memset(key->key, 0, client_query->key_length + 1); - - if (data->value == NULL) - { - - if (data->value != NULL) - { - free(data->value); - data->value = NULL; - } - if (data != NULL) - { - free(data); - data = NULL; - } - client_inactive(wi->slot); - ev_break(loop, EVBREAK_ONE); - - return; - } - - memcpy(key->key, client_query->key, client_query->key_length); - key->key[client_query->key_length + 1] = '\0'; - key->length = client_query->key_length; memset(data->value, 0, msg->length); memcpy(data->value, msg->data, msg->length); data->length = msg->length; - pgagroal_query_cache_add(cache, &(cache->table), data, key, 1); + pgagroal_query_cache_add(cache, &(cache->table), data, key); } diff --git a/src/libpgagroal/query_cache.c b/src/libpgagroal/query_cache.c index 67cab7dc..c8f2f248 100644 --- a/src/libpgagroal/query_cache.c +++ b/src/libpgagroal/query_cache.c @@ -92,18 +92,14 @@ pgagroal_query_cache_init(size_t* p_size, void** p_shmem) return 1; } struct hashEntry* -pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, struct hashEntry* key) +pgagroal_query_cache_get(struct query_cache* cache, struct hashTable** Table, char* key) { - for (int i = 0; i < cache->max_elements; i++) - { - - int x = strncmp(cache->cache[i].key->key, key->key, key->length); - - if (x == 0) - { - return cache->cache[i].data; - } + struct hashTable* s = NULL; + HASH_FIND_STR(*Table, key, s); + if (s) + { + return s->data; } return NULL; @@ -127,41 +123,25 @@ int pgagroal_query_cache_update(struct hashTable** Table, struct hashEntry* key, struct hashEntry* data) { - struct hashTable* s = NULL; - void* qkey = malloc(strlen(key->value) + 1); - strcpy(qkey, key->value); - HASH_FIND(hh, *Table, qkey, strlen(qkey), s); - HASH_DEL(*Table, s); - if (s == NULL) - { - return 0; - } - if (data->length > HASH_ENTRY_DATA_SIZE) - { - free(s); - return 0; - } - - s->data = data; - HASH_ADD_KEYPTR(hh, *Table, s->key->value, strlen(s->key->value), s); return 1; } int -pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, struct hashEntry* key, int flag) +pgagroal_query_cache_add(struct query_cache* cache, struct hashTable** Table, struct hashEntry* data, char* key) { - if (cache->max_elements >= QUERY_CACHE_MAX_ENTRIES) - { - pgagroal_log_warn("Cache is full %d", cache->max_elements); - return -1; + struct hashTable* s = NULL; + HASH_FIND_STR(*Table, key, s); + if (s) + { + return 0; } + s = (struct hashTable*)malloc(sizeof(struct hashTable)); - int idx = cache->max_elements; - - cache->cache[idx].key = key; - cache->cache[idx].data = data; - cache->max_elements = idx + 1; + memset(s->key, 0, 1024); + strcpy(s->key, key); + s->data = data; + HASH_ADD_STR(*Table, key, s); return 1;