From 795b838ff82fbe2ea4944e85f0577ff0c44b7201 Mon Sep 17 00:00:00 2001 From: Jeremy Poulin Date: Tue, 15 Jul 2014 09:23:09 -0700 Subject: [PATCH] Bug 1038357 - Added automatic pruning for mForcedValidEntries in CacheStorage Service. r=honzab --- netwerk/cache2/CacheStorageService.cpp | 33 ++++++++++++++++++++++++-- netwerk/cache2/CacheStorageService.h | 2 ++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/netwerk/cache2/CacheStorageService.cpp b/netwerk/cache2/CacheStorageService.cpp index 0ed4dd706546..2220735f0181 100644 --- a/netwerk/cache2/CacheStorageService.cpp +++ b/netwerk/cache2/CacheStorageService.cpp @@ -1067,13 +1067,42 @@ void CacheStorageService::ForceEntryValidFor(nsACString &aCacheEntryKey, { mozilla::MutexAutoLock lock(mLock); + TimeStamp now = TimeStamp::NowLoRes(); + ForcedValidEntriesPrune(now); + // This will be the timeout - TimeStamp validUntil = TimeStamp::NowLoRes() + - TimeDuration::FromSeconds(aSecondsToTheFuture); + TimeStamp validUntil = now + TimeDuration::FromSeconds(aSecondsToTheFuture); mForcedValidEntries.Put(aCacheEntryKey, validUntil); } +namespace { // anon + +PLDHashOperator PruneForcedValidEntries( + const nsACString& aKey, TimeStamp& aTimeStamp, void* aClosure) +{ + TimeStamp* now = static_cast(aClosure); + if (aTimeStamp < *now) { + return PL_DHASH_REMOVE; + } + + return PL_DHASH_NEXT; +} + +} // anon + +// Cleans out the old entries in mForcedValidEntries +void CacheStorageService::ForcedValidEntriesPrune(TimeStamp &now) +{ + static TimeDuration const oneMinute = TimeDuration::FromSeconds(60); + static TimeStamp dontPruneUntil = now + oneMinute; + if (now < dontPruneUntil) + return; + + mForcedValidEntries.Enumerate(PruneForcedValidEntries, &now); + dontPruneUntil = now + oneMinute; +} + void CacheStorageService::OnMemoryConsumptionChange(CacheMemoryConsumer* aConsumer, uint32_t aCurrentMemoryConsumption) diff --git a/netwerk/cache2/CacheStorageService.h b/netwerk/cache2/CacheStorageService.h index 6c494b2c0398..3e3e34f2e948 100644 --- a/netwerk/cache2/CacheStorageService.h +++ b/netwerk/cache2/CacheStorageService.h @@ -280,6 +280,8 @@ private: bool aReplace, CacheEntryHandle** aResult); + void ForcedValidEntriesPrune(TimeStamp &now); + static CacheStorageService* sSelf; mozilla::Mutex mLock;