@@ -1254,6 +1254,14 @@ typename NvmCache<C>::WriteHandle NvmCache<C>::createItem(
1254
1254
CacheAPIWrapperForNvm<C>::viewAsWritableChainedAllocsRange (cache_,
1255
1255
*it))) {
1256
1256
return nullptr ;
1257
+ } else {
1258
+ // Often times an object needs its associated destructor to be triggered
1259
+ // to release resources (such as memory) properly. Today we use removeCB
1260
+ // or ItemDestructor to represent this logic for cachelib's object-cache.
1261
+ // Thus, we need to ensure these callbacks are always invoked when this
1262
+ // item goes away even if the item had never been inserted into cache (aka
1263
+ // not visible to other threads).
1264
+ it.unmarkNascent ();
1257
1265
}
1258
1266
it->markNvmClean ();
1259
1267
} else {
@@ -1299,6 +1307,8 @@ template <typename C>
1299
1307
std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
1300
1308
folly::StringPiece key, const NvmItem& nvmItem, bool parentOnly) {
1301
1309
const size_t numBufs = parentOnly ? 1 : nvmItem.getNumBlobs ();
1310
+ // Only use custom cb if we are not doing parent only.
1311
+ bool useCustomCb = !parentOnly && config_.makeObjCb ;
1302
1312
// parent item
1303
1313
XDCHECK_GE (numBufs, 1u );
1304
1314
const auto pBlob = nvmItem.getBlob (0 );
@@ -1328,7 +1338,7 @@ std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
1328
1338
XDCHECK_LE (pBlob.origAllocSize , item->getSize ());
1329
1339
XDCHECK_LE (pBlob.origAllocSize , pBlob.data .size ());
1330
1340
1331
- if (!config_. makeObjCb ) {
1341
+ if (!useCustomCb ) {
1332
1342
::memcpy (item->getMemory (), pBlob.data.data(), pBlob.data.size());
1333
1343
}
1334
1344
@@ -1362,7 +1372,7 @@ std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
1362
1372
XDCHECK (chainedItem->isChainedItem ());
1363
1373
// Propagate the payload directly from Blob only if no customized callback
1364
1374
// is set.
1365
- if (!config_. makeObjCb ) {
1375
+ if (!useCustomCb ) {
1366
1376
::memcpy (chainedItem->getMemory (), cBlob.data.data(),
1367
1377
cBlob.origAllocSize);
1368
1378
}
@@ -1372,7 +1382,7 @@ std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
1372
1382
}
1373
1383
}
1374
1384
// If the customized callback is set, we'll call it to propagate the payload.
1375
- if (config_.makeObjCb ) {
1385
+ if (useCustomCb ) {
1376
1386
if (!config_.makeObjCb (nvmItem, *item,
1377
1387
viewAsWritableChainedAllocsRange (head.get ()))) {
1378
1388
return nullptr ;
0 commit comments