improved cache management to avoid clearing it unless strictly necessary

This commit is contained in:
csoler 2021-01-21 18:00:00 +01:00
parent ccd6df5c5f
commit 39a8de9bb9
2 changed files with 52 additions and 8 deletions

View File

@ -1308,8 +1308,13 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes
auto meta = locked_getMsgMeta(*c, 0); auto meta = locked_getMsgMeta(*c, 0);
if(meta) if(meta)
{
metaSet.push_back(meta); metaSet.push_back(meta);
if(mUseCache)
mMsgMetaDataCache[grpId].updateMeta(msgId,meta);
}
delete c; delete c;
} }
} }
@ -1438,8 +1443,13 @@ int RsDataService::retrieveGxsGrpMetaData(std::map<RsGxsGroupId,std::shared_ptr<
auto meta = locked_getGrpMeta(*c, 0); auto meta = locked_getGrpMeta(*c, 0);
if(meta) if(meta)
{
mit->second = meta; mit->second = meta;
if(mUseCache)
mGrpMetaDataCache.updateMeta(grpId,meta);
}
#ifdef RS_DATA_SERVICE_DEBUG_TIME #ifdef RS_DATA_SERVICE_DEBUG_TIME
++resultCount; ++resultCount;
#endif #endif
@ -1503,9 +1513,20 @@ int RsDataService::updateGroupMetaData(const GrpLocMetaData& meta)
std::cerr << (void*)this << ": erasing old entry from cache." << std::endl; std::cerr << (void*)this << ": erasing old entry from cache." << std::endl;
#endif #endif
mGrpMetaDataCache.clear(meta.grpId); if( mDb->sqlUpdate(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", meta.val))
{
RetroCursor* c = mDb->sqlQuery(GRP_TABLE_NAME, mGrpMetaColumns, "grpId='" + grpId.toStdString() + "'", "");
return mDb->sqlUpdate(GRP_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "'", meta.val) ? 1 : 0; c->moveToFirst();
auto meta = locked_getGrpMeta(*c, 0);
if(meta)
mGrpMetaDataCache.updateMeta(grpId,meta);
return 1;
}
return 0;
} }
int RsDataService::updateMessageMetaData(const MsgLocMetaData& metaData) int RsDataService::updateMessageMetaData(const MsgLocMetaData& metaData)
@ -1518,9 +1539,24 @@ int RsDataService::updateMessageMetaData(const MsgLocMetaData& metaData)
const RsGxsGroupId& grpId = metaData.msgId.first; const RsGxsGroupId& grpId = metaData.msgId.first;
const RsGxsMessageId& msgId = metaData.msgId.second; const RsGxsMessageId& msgId = metaData.msgId.second;
mMsgMetaDataCache[grpId].clear(msgId); if(mDb->sqlUpdate(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", metaData.val) )
{
// If we use the cache, update the meta data immediately.
return mDb->sqlUpdate(MSG_TABLE_NAME, KEY_GRP_ID+ "='" + grpId.toStdString() + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", metaData.val) ? 1 : 0; if(mUseCache)
{
RetroCursor* c = mDb->sqlQuery(MSG_TABLE_NAME, mMsgMetaColumns, KEY_GRP_ID+ "='" + grpId.toStdString() + "' AND " + KEY_MSG_ID + "='" + msgId.toStdString() + "'", "");
c->moveToFirst();
auto meta = locked_getMsgMeta(*c, 0);
if(meta)
mMsgMetaDataCache[grpId].updateMeta(msgId,meta);
}
return 1;
}
return 0;
} }
int RsDataService::removeMsgs(const GxsMsgReq& msgIds) int RsDataService::removeMsgs(const GxsMsgReq& msgIds)

View File

@ -79,9 +79,14 @@ public:
} }
} }
void updateMeta(const ID& id,const MetaDataClass& meta) void updateMeta(const ID& id,const MetaDataClass& meta)
{ {
mMetas[id] = std::make_shared<MetaDataClass>(meta); // create a new shared_ptr to possibly replace the previous one mMetas[id] = std::make_shared<MetaDataClass>(meta); // create a new shared_ptr to possibly replace the previous one
}
void updateMeta(const ID& id,const std::shared_ptr<MetaDataClass>& meta)
{
mMetas[id] = meta; // create a new shared_ptr to possibly replace the previous one
} }
void clear(const ID& id) void clear(const ID& id)
@ -101,8 +106,11 @@ public:
#endif #endif
mMetas.erase(it) ; mMetas.erase(it) ;
mCache_ContainsAllMetas = false;
} // No need to modify mCache_ContainsAllMetas since, assuming that the cache always contains
// all possible elements from the DB, clearing one from the cache means that it is also deleted from the db, so
// the property is preserved.
}
} }
void debug_computeSize(uint32_t& nb_items, uint64_t& total_size) const void debug_computeSize(uint32_t& nb_items, uint64_t& total_size) const