|
|
@ -90,6 +90,7 @@ int cache_delete(struct cache *cache, int keep_data) |
|
|
|
if (cache->free_cb) { |
|
|
|
cache->free_cb(entry->data); |
|
|
|
} |
|
|
|
free(entry->key); |
|
|
|
free(entry); |
|
|
|
} |
|
|
|
} |
|
|
@ -107,9 +108,12 @@ int cache_delete(struct cache *cache, int keep_data) |
|
|
|
@param key |
|
|
|
The key of the entry to remove |
|
|
|
|
|
|
|
@param key_len |
|
|
|
The length of key |
|
|
|
|
|
|
|
@return EINVAL if cache is NULL, 0 otherwise |
|
|
|
*/ |
|
|
|
int cache_remove(struct cache *cache, char *key) |
|
|
|
int cache_remove(struct cache *cache, char *key, size_t key_len) |
|
|
|
{ |
|
|
|
struct cache_entry *tmp; |
|
|
|
|
|
|
@ -117,13 +121,14 @@ int cache_remove(struct cache *cache, char *key) |
|
|
|
return EINVAL; |
|
|
|
} |
|
|
|
|
|
|
|
HASH_FIND_STR(cache->entries, key, tmp); |
|
|
|
HASH_FIND(hh, cache->entries, key, key_len, tmp); |
|
|
|
|
|
|
|
if (tmp) { |
|
|
|
HASH_DEL(cache->entries, tmp); |
|
|
|
if (cache->free_cb) { |
|
|
|
cache->free_cb(tmp->data); |
|
|
|
} |
|
|
|
free(tmp->key); |
|
|
|
free(tmp); |
|
|
|
} |
|
|
|
|
|
|
@ -138,6 +143,9 @@ int cache_remove(struct cache *cache, char *key) |
|
|
|
@param key |
|
|
|
The key to look-up |
|
|
|
|
|
|
|
@param key_len |
|
|
|
The length of key |
|
|
|
|
|
|
|
@param result |
|
|
|
Where to store the result if key is found. |
|
|
|
|
|
|
@ -147,7 +155,7 @@ int cache_remove(struct cache *cache, char *key) |
|
|
|
|
|
|
|
@return EINVAL if cache is NULL, 0 otherwise |
|
|
|
*/ |
|
|
|
int cache_lookup(struct cache *cache, char *key, void *result) |
|
|
|
int cache_lookup(struct cache *cache, char *key, size_t key_len, void *result) |
|
|
|
{ |
|
|
|
struct cache_entry *tmp = NULL; |
|
|
|
char **dirty_hack = result; |
|
|
@ -156,9 +164,8 @@ int cache_lookup(struct cache *cache, char *key, void *result) |
|
|
|
return EINVAL; |
|
|
|
} |
|
|
|
|
|
|
|
HASH_FIND_STR(cache->entries, key, tmp); |
|
|
|
HASH_FIND(hh, cache->entries, key, key_len, tmp); |
|
|
|
if (tmp) { |
|
|
|
size_t key_len = strnlen(tmp->key, KEY_MAX_LENGTH); |
|
|
|
HASH_DELETE(hh, cache->entries, tmp); |
|
|
|
HASH_ADD_KEYPTR(hh, cache->entries, tmp->key, key_len, tmp); |
|
|
|
*dirty_hack = tmp->data; |
|
|
@ -169,6 +176,26 @@ int cache_lookup(struct cache *cache, char *key, void *result) |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int cache_key_exist(struct cache *cache, char *key, size_t key_len) |
|
|
|
{ |
|
|
|
struct cache_entry *tmp = NULL; |
|
|
|
|
|
|
|
if (!cache || !key) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
HASH_FIND(hh, cache->entries, key, key_len, tmp); |
|
|
|
if (tmp) { |
|
|
|
HASH_DELETE(hh, cache->entries, tmp); |
|
|
|
HASH_ADD_KEYPTR(hh, cache->entries, tmp->key, key_len, tmp); |
|
|
|
return 1; |
|
|
|
} else { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
/** Inserts a given <key, value> pair into the cache |
|
|
|
|
|
|
|
@param cache |
|
|
@ -177,16 +204,18 @@ int cache_lookup(struct cache *cache, char *key, void *result) |
|
|
|
@param key |
|
|
|
The key that identifies <value> |
|
|
|
|
|
|
|
@param key_len |
|
|
|
The length of key |
|
|
|
|
|
|
|
@param data |
|
|
|
Data associated with <key> |
|
|
|
|
|
|
|
@return EINVAL if cache is NULL, ENOMEM if malloc fails, 0 otherwise |
|
|
|
*/ |
|
|
|
int cache_insert(struct cache *cache, char *key, void *data) |
|
|
|
int cache_insert(struct cache *cache, char *key, size_t key_len, void *data) |
|
|
|
{ |
|
|
|
struct cache_entry *entry = NULL; |
|
|
|
struct cache_entry *tmp_entry = NULL; |
|
|
|
size_t key_len = 0; |
|
|
|
|
|
|
|
if (!cache || !data) { |
|
|
|
return EINVAL; |
|
|
@ -196,9 +225,9 @@ int cache_insert(struct cache *cache, char *key, void *data) |
|
|
|
return ENOMEM; |
|
|
|
} |
|
|
|
|
|
|
|
entry->key = key; |
|
|
|
entry->key = malloc(key_len); |
|
|
|
mempcpy(entry->key, key, key_len); |
|
|
|
entry->data = data; |
|
|
|
key_len = strnlen(entry->key, KEY_MAX_LENGTH); |
|
|
|
HASH_ADD_KEYPTR(hh, cache->entries, entry->key, key_len, entry); |
|
|
|
|
|
|
|
if (HASH_COUNT(cache->entries) >= cache->max_entries) { |
|
|
@ -209,7 +238,7 @@ int cache_insert(struct cache *cache, char *key, void *data) |
|
|
|
} else { |
|
|
|
free(entry->data); |
|
|
|
} |
|
|
|
/* free(key->key) if data has been copied */ |
|
|
|
free(entry->key); |
|
|
|
free(entry); |
|
|
|
break; |
|
|
|
} |
|
|
|