| 100 | } |
| 101 | |
| 102 | void ProcessCache::ClearExpiredCache() { |
| 103 | { |
| 104 | std::lock_guard<std::mutex> lock(mCacheExpireQueueMutex); |
| 105 | mCacheExpireQueueProcessing.swap(mCacheExpireQueue); |
| 106 | } |
| 107 | if (mCacheExpireQueueProcessing.empty()) { |
| 108 | return; |
| 109 | } |
| 110 | size_t nextQueueSize = 0; |
| 111 | for (auto& entry : mCacheExpireQueueProcessing) { |
| 112 | if (entry.value->LifeStage() == ProcessCacheValue::LifeStage::kDeleted) { |
| 113 | LOG_WARNING(sLogger, ("clear expired cache twice pid", entry.key.pid)("ktime", entry.key.time)); |
| 114 | continue; |
| 115 | } |
| 116 | if (entry.value->RefCount() > 0) { |
| 117 | entry.value->SetLifeStage(ProcessCacheValue::LifeStage::kInUse); |
| 118 | continue; |
| 119 | } |
| 120 | if (entry.value->LifeStage() == ProcessCacheValue::LifeStage::kDeletePending) { |
| 121 | entry.value->SetLifeStage(ProcessCacheValue::LifeStage::kDeleteReady); |
| 122 | mCacheExpireQueueProcessing[nextQueueSize++] = entry; |
| 123 | continue; |
| 124 | } |
| 125 | if (entry.value->LifeStage() == ProcessCacheValue::LifeStage::kDeleteReady) { |
| 126 | entry.value->SetLifeStage(ProcessCacheValue::LifeStage::kDeleted); |
| 127 | LOG_DEBUG(sLogger, ("clear expired cache pid", entry.key.pid)("ktime", entry.key.time)); |
| 128 | removeCache(entry.key); |
| 129 | } |
| 130 | } |
| 131 | if (nextQueueSize > 0) { |
| 132 | mCacheExpireQueueProcessing.resize(nextQueueSize); |
| 133 | mCacheExpireQueue.insert(mCacheExpireQueue.end(), |
| 134 | std::make_move_iterator(mCacheExpireQueueProcessing.begin()), |
| 135 | std::make_move_iterator(mCacheExpireQueueProcessing.end())); |
| 136 | } |
| 137 | mCacheExpireQueueProcessing.clear(); |
| 138 | } |
| 139 | |
| 140 | void ProcessCache::ForceShrink() { |
| 141 | if (mLastForceShrinkTimeSec != 0 && mLastForceShrinkTimeSec > TimeKeeper::GetInstance()->NowSec() - 120) { |