| 76 | } |
| 77 | |
| 78 | void *Cache_GetValue(Cache *cache, const char *key) { |
| 79 | void *item = NULL; |
| 80 | |
| 81 | ASSERT(cache != NULL); |
| 82 | |
| 83 | int res = pthread_rwlock_rdlock(&cache->_cache_rwlock); |
| 84 | UNUSED(res); |
| 85 | ASSERT(res == 0); |
| 86 | |
| 87 | size_t key_len = strlen(key); |
| 88 | CacheEntry *entry = raxFind(cache->lookup, (unsigned char *)key, key_len); |
| 89 | |
| 90 | if(entry == raxNotFound) goto cleanup; |
| 91 | |
| 92 | // element is now the most recently used; update its LRU |
| 93 | // note that multiple threads can be here simultaneously |
| 94 | cache->counter++; |
| 95 | entry->LRU = cache->counter; |
| 96 | |
| 97 | // return a copy of element |
| 98 | item = cache->copy_item(entry->value); |
| 99 | |
| 100 | cleanup: |
| 101 | res = pthread_rwlock_unlock(&cache->_cache_rwlock); |
| 102 | ASSERT(res == 0); |
| 103 | return item; |
| 104 | } |
| 105 | |
| 106 | void Cache_SetValue(Cache *cache, const char *key, void *value) { |
| 107 | ASSERT(key != NULL); |
no outgoing calls