Fixed cache invalidation issue

This commit is contained in:
2025-06-22 16:49:56 +02:00
parent de7e145b89
commit dc9c4a6279
4 changed files with 26 additions and 12 deletions

View File

@@ -56,10 +56,10 @@ public class EntityController : ControllerBase
foreach (var jsonEntity in jsonEntities) foreach (var jsonEntity in jsonEntities)
{ {
string jsonEntityName = jsonEntity.Name; string jsonEntityName = jsonEntity.Name;
if (entities.Select(x => x.name == jsonEntityName).Any()
&& !invalidatedSearchdomains.Contains(jsonEntityName))
{
string jsonEntitySearchdomainName = jsonEntity.Searchdomain; string jsonEntitySearchdomainName = jsonEntity.Searchdomain;
if (entities.Select(x => x.name == jsonEntityName).Any()
&& !invalidatedSearchdomains.Contains(jsonEntitySearchdomainName))
{
invalidatedSearchdomains.Add(jsonEntitySearchdomainName); invalidatedSearchdomains.Add(jsonEntitySearchdomainName);
_domainManager.InvalidateSearchdomainCache(jsonEntitySearchdomainName); _domainManager.InvalidateSearchdomainCache(jsonEntitySearchdomainName);
} }

View File

@@ -118,8 +118,24 @@ public static class SearchdomainHelper
foreach (JSONDatapoint jsonDatapoint in jsonEntity.Datapoints) foreach (JSONDatapoint jsonDatapoint in jsonEntity.Datapoints)
{ {
string hash = Convert.ToBase64String(SHA256.HashData(Encoding.UTF8.GetBytes(jsonDatapoint.Text))); string hash = Convert.ToBase64String(SHA256.HashData(Encoding.UTF8.GetBytes(jsonDatapoint.Text)));
Dictionary<string, float[]> embeddings = embeddingsLUT.ContainsKey(hash) ? embeddingsLUT[hash] : []; Dictionary<string, float[]> embeddings = [];
if (embeddings.Count == 0) if (embeddingsLUT.ContainsKey(hash))
{
Dictionary<string, float[]> hashLUT = embeddingsLUT[hash];
foreach (string model in jsonDatapoint.Model)
{
if (hashLUT.ContainsKey(model))
{
embeddings.Add(model, hashLUT[model]);
}
else
{
var additionalEmbeddings = Datapoint.GenerateEmbeddings(jsonDatapoint.Text, [model], ollama, embeddingCache);
embeddings.Add(model, additionalEmbeddings.First().Value);
}
}
}
else
{ {
embeddings = Datapoint.GenerateEmbeddings(jsonDatapoint.Text, [.. jsonDatapoint.Model], ollama, embeddingCache); embeddings = Datapoint.GenerateEmbeddings(jsonDatapoint.Text, [.. jsonDatapoint.Model], ollama, embeddingCache);
} }

View File

@@ -65,7 +65,6 @@ public class Searchdomain
public void UpdateEntityCache() public void UpdateEntityCache()
{ {
entityCache = [];
Dictionary<string, dynamic> parametersIDSearchdomain = new() Dictionary<string, dynamic> parametersIDSearchdomain = new()
{ {
["id"] = this.id ["id"] = this.id
@@ -132,6 +131,7 @@ public class Searchdomain
} }
attributeReader.Close(); attributeReader.Close();
entityCache = [];
DbDataReader entityReader = helper.ExecuteSQLCommand("SELECT entity.id, name, probmethod FROM entity WHERE id_searchdomain=@id", parametersIDSearchdomain); DbDataReader entityReader = helper.ExecuteSQLCommand("SELECT entity.id, name, probmethod FROM entity WHERE id_searchdomain=@id", parametersIDSearchdomain);
while (entityReader.Read()) while (entityReader.Read())
{ {
@@ -155,6 +155,7 @@ public class Searchdomain
} }
entityReader.Close(); entityReader.Close();
modelsInUse = GetModels(entityCache); modelsInUse = GetModels(entityCache);
embeddingCache = []; // TODO remove this and implement proper remediation to improve performance
} }
public List<(float, string)> Search(string query, bool sort=true) public List<(float, string)> Search(string query, bool sort=true)
@@ -166,7 +167,7 @@ public class Searchdomain
{ // Idea: Add access count to each entry. On limit hit, sort the entries by access count and remove the bottom 10% of entries { // Idea: Add access count to each entry. On limit hit, sort the entries by access count and remove the bottom 10% of entries
embeddingCache.Add(query, queryEmbeddings); embeddingCache.Add(query, queryEmbeddings);
} }
} } // TODO implement proper cache remediation for embeddingCache here
List<(float, string)> result = []; List<(float, string)> result = [];

View File

@@ -64,10 +64,7 @@ public class SearchdomainManager
public void InvalidateSearchdomainCache(string searchdomainName) public void InvalidateSearchdomainCache(string searchdomainName)
{ {
if (searchdomains.TryGetValue(searchdomainName, out var searchdomain)) GetSearchdomain(searchdomainName).UpdateEntityCache();
{
searchdomain.UpdateEntityCache();
}
} }
public List<string> ListSearchdomains() public List<string> ListSearchdomains()