| 342 | |
| 343 | template<Cache::EvictionPolicy policy> |
| 344 | void RLCacheShard<policy>::UpdateCharge(HandleBase* handle, size_t charge) { |
| 345 | DCHECK(initialized_); |
| 346 | |
| 347 | // Check for eviction based on new usage |
| 348 | RLThreadState tstate; |
| 349 | { |
| 350 | // Hold the lock to avoid concurrent evictions |
| 351 | std::lock_guard<decltype(mutex_)> l(mutex_); |
| 352 | RLHandle* e = static_cast<RLHandle*>(handle); |
| 353 | // If the handle is not part of the cache (i.e. it was evicted), |
| 354 | // then we shouldn't update the charge. The caller has a handle to |
| 355 | // the entry, so it was previously in the cache. |
| 356 | if (e->next == nullptr) return; |
| 357 | int64_t delta = charge - handle->charge(); |
| 358 | handle->set_charge(charge); |
| 359 | UpdateMemTracker(delta); |
| 360 | usage_ += delta; |
| 361 | |
| 362 | // Evict entries as needed to enforce the capacity constraint |
| 363 | EnforceCapacity(&tstate); |
| 364 | } |
| 365 | |
| 366 | // we free the entries here outside of mutex for performance reasons |
| 367 | CleanupThreadState(&tstate); |
| 368 | } |
| 369 | |
| 370 | template<Cache::EvictionPolicy policy> |
| 371 | void RLCacheShard<policy>::Release(HandleBase* handle) { |