add cleanInstanceCache

This commit is contained in:
pa
2026-01-20 23:52:46 +09:00
committed by Natsumi
parent ecbb0612ec
commit f8daa6ff4c
2 changed files with 28 additions and 1 deletions

View File

@@ -55,6 +55,32 @@ export const useInstanceStore = defineStore('Instance', () => {
let cachedInstances = new Map(); let cachedInstances = new Map();
function cleanInstanceCache() {
const maxSize = 200;
if (cachedInstances.size <= maxSize) {
return;
}
const removable = [];
cachedInstances.forEach((ref, id) => {
if (
[...friendStore.friends.values()].some(
(f) => f.$location?.tag === id
)
) {
return;
}
removable.push({
id,
fetchedAt: Date.parse(ref.$fetchedAt) || 0
});
});
removable.sort((a, b) => a.fetchedAt - b.fetchedAt);
const overBy = cachedInstances.size - maxSize;
for (let i = 0; i < overBy && i < removable.length; i++) {
cachedInstances.delete(removable[i].id);
}
}
const lastInstanceApplied = ref(''); const lastInstanceApplied = ref('');
const currentInstanceWorld = ref({ const currentInstanceWorld = ref({
@@ -326,6 +352,7 @@ export const useInstanceStore = defineStore('Instance', () => {
...json ...json
}; };
cachedInstances.set(ref.id, ref); cachedInstances.set(ref.id, ref);
cleanInstanceCache();
} else { } else {
Object.assign(ref, json); Object.assign(ref, json);
} }

View File

@@ -411,7 +411,7 @@ export const useUserStore = defineStore('User', () => {
* @param {Map<string, any>} friendMap * @param {Map<string, any>} friendMap
*/ */
function cleanupUserCache(userCache, friendMap) { function cleanupUserCache(userCache, friendMap) {
const bufferSize = 200; const bufferSize = 300;
const currentFriendCount = friendMap.size; const currentFriendCount = friendMap.size;
const currentTotalSize = userCache.size; const currentTotalSize = userCache.size;