| 263 | } |
| 264 | |
| 265 | folly::coro::Task<UnitResult> RAMCacheComponent::insert( |
| 266 | AllocatedHandle&& handle) { |
| 267 | stats_->insert_.throughput_.calls_.inc(); |
| 268 | |
| 269 | if (!handle) { |
| 270 | stats_->insert_.throughput_.errors_.inc(); |
| 271 | co_return makeError(Error::Code::INVALID_ARGUMENTS, |
| 272 | "empty AllocatedHandle"); |
| 273 | } |
| 274 | |
| 275 | auto latencyGuard = stats_->insert_.latency_.start(); |
| 276 | try { |
| 277 | auto* implItem = getImplItemFromHandle(handle); |
| 278 | auto implHandle = cache_->acquire(implItem); |
| 279 | if (!implHandle) { |
| 280 | XLOG(DFATAL) << "Evicting item that hasn't been inserted"; |
| 281 | stats_->insert_.throughput_.errors_.inc(); |
| 282 | co_return makeError(Error::Code::INSERT_FAILED, "item is being evicted"); |
| 283 | } |
| 284 | if (!cache_->insert(implHandle)) { |
| 285 | stats_->insert_.throughput_.errors_.inc(); |
| 286 | co_return makeError(Error::Code::ALREADY_INSERTED, |
| 287 | "key already exists in cache"); |
| 288 | } |
| 289 | } catch (const std::invalid_argument& ia) { |
| 290 | // Should only happen when implHandle->isAccessible() == true |
| 291 | XLOG(DFATAL) << "Double-insert from the same allocated handle - did we " |
| 292 | "forget to release the AllocatedHandle after inserting?"; |
| 293 | stats_->insert_.throughput_.errors_.inc(); |
| 294 | co_return makeError(Error::Code::ALREADY_INSERTED, ia.what()); |
| 295 | } catch (const std::exception& e) { |
| 296 | stats_->insert_.throughput_.errors_.inc(); |
| 297 | co_return makeError(Error::Code::INSERT_FAILED, e.what()); |
| 298 | } |
| 299 | |
| 300 | stats_->insert_.throughput_.successes_.inc(); |
| 301 | auto _ = std::move(handle); |
| 302 | co_return folly::unit; |
| 303 | } |
| 304 | |
| 305 | folly::coro::Task<Result<std::optional<AllocatedHandle>>> |
| 306 | RAMCacheComponent::insertOrReplace(AllocatedHandle&& handle) { |