add cleanup function for user ccache and world cache

This commit is contained in:
pa
2025-10-31 19:00:09 +09:00
committed by Natsumi
parent 69923c2655
commit 0d3c1f646b
2 changed files with 62 additions and 0 deletions

View File

@@ -389,6 +389,49 @@ export const useUserStore = defineStore('User', () => {
}
const robotUrl = `${AppDebug.endpointDomain}/file/file_0e8c4e32-7444-44ea-ade4-313c010d4bae/1/file`;
/**
*
* @param {Map<string, any>} userCache
* @param {Map<string, any>} 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 {

View File

@@ -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);