Remove key hashing and explicitly clear the cache after printing characters

This commit is contained in:
Joe
2024-11-24 14:54:12 -08:00
parent d1cac298c6
commit e1d6a47048
2 changed files with 11 additions and 18 deletions

View File

@ -268,6 +268,7 @@ import { initServerHistory } from './scripts/server-history.js';
import { initSettingsSearch } from './scripts/setting-search.js'; import { initSettingsSearch } from './scripts/setting-search.js';
import { initBulkEdit } from './scripts/bulk-edit.js'; import { initBulkEdit } from './scripts/bulk-edit.js';
import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js'; import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
import { clearFuzzySearchCaches } from './scripts/power-user.js';
//exporting functions and vars for mods //exporting functions and vars for mods
export { export {
@ -1515,6 +1516,7 @@ export async function printCharacters(fullRefresh = false) {
}); });
favsToHotswap(); favsToHotswap();
clearFuzzySearchCaches();
} }
/** Checks the state of the current search, and adds/removes the search sorting option accordingly */ /** Checks the state of the current search, and adds/removes the search sorting option accordingly */

View File

@ -329,11 +329,11 @@ let browser_has_focus = true;
const debug_functions = []; const debug_functions = [];
const fuzzySearchCaches = { const fuzzySearchCaches = {
characters: { keyHash: null, resultMap: new Map() }, characters: { resultMap: new Map() },
worldInfo: { keyHash: null, resultMap: new Map() }, worldInfo: { resultMap: new Map() },
personas: { keyHash: null, resultMap: new Map() }, personas: { resultMap: new Map() },
tags: { keyHash: null, resultMap: new Map() }, tags: { resultMap: new Map() },
groups: { keyHash: null, resultMap: new Map() }, groups: { resultMap: new Map() },
}; };
const fuzzySearchCategories = { const fuzzySearchCategories = {
@ -1864,22 +1864,14 @@ function hashFuseKeys(keys) {
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
*/ */
function performFuzzySearch(type, data, keys, searchValue) { function performFuzzySearch(type, data, keys, searchValue) {
const currentKeyHash = hashFuseKeys(keys);
const cache = fuzzySearchCaches[type]; const cache = fuzzySearchCaches[type];
// Check cache for existing results // Check cache for existing results
if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) { if (cache.resultMap.has(searchValue)) {
// console.debug(`Using cached ${type} fuzzy search results for ${searchValue}`);
return cache.resultMap.get(searchValue); return cache.resultMap.get(searchValue);
} }
// Clear cache if keys changed // @ts-ignore
if (cache.keyHash !== currentKeyHash) {
// console.debug(`${type} Fuse keys changed, clearing cache`);
cache.keyHash = currentKeyHash;
cache.resultMap.clear();
}
const fuse = new Fuse(data, { const fuse = new Fuse(data, {
keys: keys, keys: keys,
includeScore: true, includeScore: true,
@ -1890,19 +1882,18 @@ function performFuzzySearch(type, data, keys, searchValue) {
const results = fuse.search(searchValue); const results = fuse.search(searchValue);
cache.resultMap.set(searchValue, results); cache.resultMap.set(searchValue, results);
// console.debug(`${type} fuzzy search results for ${searchValue}`, results);
return results; return results;
} }
/** /**
* Clears all fuzzy search caches * Clears all fuzzy search caches
*/ */
export function clearFuzzySearchCaches() { export function clearFuzzySearchCaches() {
for (const cache of Object.values(fuzzySearchCaches)) { for (const cache of Object.values(fuzzySearchCaches)) {
cache.keyHash = null;
cache.resultMap.clear(); cache.resultMap.clear();
} }
console.log('Fuzzy search caches cleared');
} }
/** /**