| 379 | |
| 380 | template<Cache::EvictionPolicy policy> |
| 381 | HandleBase* RLCacheShard<policy>::Insert( |
| 382 | HandleBase* handle_in, |
| 383 | Cache::EvictionCallback* eviction_callback) { |
| 384 | DCHECK(initialized_); |
| 385 | RLHandle* handle = static_cast<RLHandle*>(handle_in); |
| 386 | // Set the remaining RLHandle members which were not already allocated during |
| 387 | // Allocate(). |
| 388 | handle->eviction_callback = eviction_callback; |
| 389 | // Two refs for the handle: one from RLCacheShard, one for the returned handle. |
| 390 | handle->refs.store(2, std::memory_order_relaxed); |
| 391 | UpdateMemTracker(handle->charge()); |
| 392 | |
| 393 | RLThreadState tstate; |
| 394 | { |
| 395 | std::lock_guard<decltype(mutex_)> l(mutex_); |
| 396 | |
| 397 | RL_Append(handle); |
| 398 | |
| 399 | RLHandle* old = static_cast<RLHandle*>(table_.Insert(handle)); |
| 400 | if (old != nullptr) { |
| 401 | RL_Remove(old); |
| 402 | if (Unref(old)) { |
| 403 | old->next = tstate.to_remove_head; |
| 404 | tstate.to_remove_head = old; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // Evict entries as needed to enforce the capacity constraint |
| 409 | EnforceCapacity(&tstate); |
| 410 | } |
| 411 | |
| 412 | // we free the entries here outside of mutex for |
| 413 | // performance reasons |
| 414 | CleanupThreadState(&tstate); |
| 415 | |
| 416 | return handle; |
| 417 | } |
| 418 | |
| 419 | template<Cache::EvictionPolicy policy> |
| 420 | void RLCacheShard<policy>::Erase(const Slice& key, uint32_t hash) { |