diff --git a/src/stores/user.js b/src/stores/user.js index 5f5df520..4e9086c4 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -389,6 +389,49 @@ export const useUserStore = defineStore('User', () => { } const robotUrl = `${AppDebug.endpointDomain}/file/file_0e8c4e32-7444-44ea-ade4-313c010d4bae/1/file`; + + /** + * + * @param {Map} userCache + * @param {Map} friendMap + */ + function cleanupUserCache(userCache, friendMap) { + const bufferSize = 200; + + const currentFriendCount = friendMap.size; + const currentTotalSize = userCache.size; + + const effectiveMaxSize = currentFriendCount + bufferSize; + + if (currentTotalSize <= effectiveMaxSize) { + return; + } + + const targetDeleteCount = currentTotalSize - effectiveMaxSize; + let deletedCount = 0; + const keysToDelete = []; + + for (const userId of userCache.keys()) { + if (friendMap.has(userId)) { + continue; + } + + if (deletedCount >= targetDeleteCount) { + break; + } + + keysToDelete.push(userId); + deletedCount++; + } + + for (const id of keysToDelete) { + userCache.delete(id); + } + + console.log( + `User cache cleanup: Deleted ${deletedCount}. Current cache size: ${userCache.size}` + ); + } /** * * @param {import('../types/api/user').GetUserResponse} json @@ -506,6 +549,7 @@ export const useUserStore = defineStore('User', () => { ref.$customTag = ''; ref.$customTagColour = ''; } + cleanupUserCache(cachedUsers, friendStore.friends); cachedUsers.set(ref.id, ref); friendStore.updateFriend(ref.id); } else { diff --git a/src/stores/world.js b/src/stores/world.js index fc38d97e..cae45710 100644 --- a/src/stores/world.js +++ b/src/stores/world.js @@ -215,6 +215,23 @@ export const useWorldStore = defineStore('World', () => { } } + function cleanupWorldCache(WorldCache) { + const maxCacheSize = 10000; + + if (WorldCache.size <= maxCacheSize) { + return; + } + + const deletedCount = WorldCache.size - maxCacheSize; + while (WorldCache.size > maxCacheSize) { + const deletedKey = WorldCache.keys().next().value; + WorldCache.delete(deletedKey); + } + console.log( + `World cache cleanup: Deleted ${deletedCount}. Current cache size: ${WorldCache.size}` + ); + } + /** * * @param {object} json @@ -270,6 +287,7 @@ export const useWorldStore = defineStore('World', () => { // ...json }; + cleanupWorldCache(cachedWorlds); cachedWorlds.set(ref.id, ref); } else { Object.assign(ref, json);