Added search cache estimation, added search cache clearing

This commit is contained in:
2025-12-19 10:07:22 +01:00
parent 94323e1f72
commit ca24dc04ab
4 changed files with 191 additions and 2 deletions

View File

@@ -176,4 +176,52 @@ public class SearchdomainController : ControllerBase
SearchdomainSettings settings = searchdomain_.settings;
return Ok(new SearchdomainSettingsResults() { Settings = settings, Success = true });
}
[HttpGet("GetSearchCacheSize")]
public ActionResult<SearchdomainSearchCacheSizeResults> GetSearchCacheSize(string searchdomain)
{
Searchdomain searchdomain_;
try
{
searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
}
catch (SearchdomainNotFoundException)
{
_logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]);
return Ok(new SearchdomainSearchCacheSizeResults() { SearchCacheSizeBytes = null, Success = false, Message = "Searchdomain not found" });
}
catch (Exception ex)
{
_logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]);
return Ok(new SearchdomainSearchCacheSizeResults() { SearchCacheSizeBytes = null, Success = false, Message = ex.Message });
}
Dictionary<string, DateTimedSearchResult> searchCache = searchdomain_.searchCache;
long sizeInBytes = 0;
foreach (var entry in searchCache)
{
sizeInBytes += sizeof(int); // string length prefix
sizeInBytes += entry.Key.Length * sizeof(char); // string characters
sizeInBytes += entry.Value.EstimateSize();
}
return Ok(new SearchdomainSearchCacheSizeResults() { SearchCacheSizeBytes = sizeInBytes, Success = true });
}
[HttpGet("ClearSearchCache")]
public ActionResult<SearchdomainInvalidateCacheResults> InvalidateSearchCache(string searchdomain)
{
try
{
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
searchdomain_.InvalidateSearchCache();
} catch (SearchdomainNotFoundException)
{
_logger.LogError("Unable to invalidate search cache for searchdomain {searchdomain} - not found", [searchdomain]);
return Ok(new SearchdomainInvalidateCacheResults() { Success = false, Message = $"Unable to invalidate search cache for searchdomain {searchdomain} - not found" });
} catch (Exception ex)
{
_logger.LogError("Unable to invalidate search cache for searchdomain {searchdomain} - Exception: {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]);
return Ok(new SearchdomainInvalidateCacheResults() { Success = false, Message = $"Unable to invalidate search cache for searchdomain {searchdomain}" });
}
return Ok(new SearchdomainInvalidateCacheResults(){Success = true});
}
}

View File

@@ -68,9 +68,9 @@
<!-- Cache -->
<div class="d-flex align-items-center mb-4">
<div class="me-3">
<strong>Cache utilization:</strong> 2.47MiB
<strong>Cache utilization:</strong> <span id="cacheUtilization">0.00MiB</span>
</div>
<button class="btn btn-primary btn-sm">Reset</button>
<button id="cacheClear" class="btn btn-warning btn-sm">@T["Clear"]</button>
</div>
<!-- Recent Queries -->
@@ -405,6 +405,27 @@
console.error('Error creating searchdomain:', error);
});
});
document
.getElementById('cacheClear')
.addEventListener('click', () => {
const domainKey = getSelectedDomainKey();
fetch(`/Searchdomain/ClearSearchCache?searchdomain=${encodeURIComponent(domains[domainKey])}`, {
method: 'GET'
}).then(response => {
if (response.ok) {
// TODO add toast
console.log('Searchdomain cache cleared successfully');
// Update cache utilization display
document.querySelector('#cacheUtilization').innerText = '0.00MiB';
} else {
// TODO add toast
console.error('Failed to clear searchdomain cache');
}
}).catch(error => {
console.error('Error clearing searchdomain cache:', error);
});
});
});
function deleteSearchdomain(domainKey) {
@@ -480,6 +501,11 @@
.then(r => r.json());
}
function getSearchdomainCacheUtilization(domainKey) {
return fetch(`/Searchdomain/GetSearchCacheSize?searchdomain=${encodeURIComponent(domains[domainKey])}`)
.then(r => r.json());
}
function selectDomain(domainKey) {
document.querySelectorAll('.domain-item').forEach(item => {
item.classList.remove('active');
@@ -494,6 +520,8 @@
let searchdomainConfigPromise = getSearchdomainConfig(getSelectedDomainKey());
let configElementCacheReconsiliation = document.getElementById('searchdomainConfigCacheReconciliation');
let cacheUtilizationPromise = getSearchdomainCacheUtilization(getSelectedDomainKey());
/* ---------- ENTITIES ---------- */
let entitiesUrl = `/Entity/List?searchdomain=${encodeURIComponent(domainName)}&returnEmbeddings=false`;
let entitiesCard = document.querySelector("#entitiesTable").parentElement;
@@ -544,6 +572,17 @@
console.error('Failed to fetch searchdomain config');
}
});
cacheUtilizationPromise.then(cacheUtilization => {
if (cacheUtilization != null && cacheUtilization.SearchCacheSizeBytes != null)
{
console.log(cacheUtilization);
document.querySelector('#cacheUtilization').innerText =
`${(cacheUtilization.SearchCacheSizeBytes / (1024 * 1024)).toFixed(2)}MiB`;
} else {
// TODO add toast
console.error('Failed to fetch searchdomain cache utilization');
}
});
}
function clearEntitiesTable() {