From 03b2fb829fe60d43d8c4730e8b3ab3113cf22a46 Mon Sep 17 00:00:00 2001 From: wuxu06 Date: Mon, 2 Sep 2024 17:24:09 +0800 Subject: [PATCH] Fixed the issue: The total cost is incorrect when setting same identical content. --- TMCache/TMMemoryCache.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TMCache/TMMemoryCache.m b/TMCache/TMMemoryCache.m index 17c56a4..e8f53f7 100644 --- a/TMCache/TMMemoryCache.m +++ b/TMCache/TMMemoryCache.m @@ -286,6 +286,10 @@ - (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost bl if (strongSelf->_willAddObjectBlock) strongSelf->_willAddObjectBlock(strongSelf, key, object); + NSNumber *oldCost = [strongSelf->_costs objectForKey:key]; + if (oldCost) + _totalCost -= [oldCost unsignedIntegerValue]; + [strongSelf->_dictionary setObject:object forKey:key]; [strongSelf->_dates setObject:now forKey:key]; [strongSelf->_costs setObject:@(cost) forKey:key];