| 21 | } |
| 22 | |
| 23 | static bool _Cache_SetValue(Cache *cache, const char *key, void *value, |
| 24 | size_t key_len) { |
| 25 | ASSERT(key != NULL); |
| 26 | ASSERT(cache != NULL); |
| 27 | |
| 28 | /* in case that another working thread had already inserted the item to the |
| 29 | * cache, no need to re-insert it */ |
| 30 | CacheEntry *entry = raxFind(cache->lookup, (unsigned char *)key, key_len); |
| 31 | if(entry != raxNotFound) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | // key is not in cache! test to see if cache is full? |
| 36 | if(cache->size == cache->cap) { |
| 37 | /* the cache is full, evict the least-recently-used element |
| 38 | * and reuse its space for the new element */ |
| 39 | entry = _CacheEvictLRU(cache); |
| 40 | } else { |
| 41 | // the array has space left in it, use the next available entry |
| 42 | entry = cache->arr + cache->size++; |
| 43 | } |
| 44 | |
| 45 | // populate the entry |
| 46 | char *k = rm_strdup(key); |
| 47 | cache->counter++; |
| 48 | CacheArray_PopulateEntry(cache->counter, entry, k, value); |
| 49 | |
| 50 | |
| 51 | // Add the new entry to the rax. |
| 52 | raxInsert(cache->lookup, (unsigned char *)key, key_len, entry, NULL); |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | Cache *Cache_New(uint cap, CacheEntryFreeFunc freeFunc, CacheEntryCopyFunc copyFunc) { |
| 58 | ASSERT(cap > 0); |
no test coverage detected