From 6a505bb766bdb73966970c1ba02a1758a6c31f18 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Fri, 21 Aug 2020 19:53:53 +0100 Subject: [PATCH] Can't iterate through a dict and remove items Now adding keys to be removed into an array and then popping those keys while iterating through array --- bot/libs/cache.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bot/libs/cache.py b/bot/libs/cache.py index e42aca7a..235eff7f 100644 --- a/bot/libs/cache.py +++ b/bot/libs/cache.py @@ -95,6 +95,9 @@ class MyCoolCache: def remove_many(self, in_guild_id): # This method is to be used for when the bot has left a guild with self.threadLock: + # Array to store keys to be removed + keys_to_remove = [] + # For every member within the cache for (member_id, guild_id) in self.cache: # if the guild_id passed in is equal to the guild_id within the cache @@ -102,5 +105,10 @@ class MyCoolCache: # When removing a value from the cache due to a guild leave, permanently remove all values # Yes it is expensive, however as this can run concurrently and we won't need the data available # For this guild, it doesn't matter how long it takes, and will save in memory in the long term - self.cache.pop((member_id, guild_id)) + # Store key within array + keys_to_remove.append((member_id, guild_id)) self.queue.remove((member_id, guild_id)) + + # Iterate through the array and then pop the keys from cache + for key in keys_to_remove: + self.cache.pop(key)