diff --git a/src/Server/Models/SearchdomainModels.cs b/src/Server/Models/SearchdomainModels.cs new file mode 100644 index 0000000..e91a755 --- /dev/null +++ b/src/Server/Models/SearchdomainModels.cs @@ -0,0 +1,19 @@ +using MySqlX.XDevAPI.Common; + +namespace Server.Models; +public readonly struct ResultItem(float score, string name) +{ + public readonly float Score = score; + public readonly string Name = name; +} + +public struct DateTimedSearchResult +{ + public List accessTimes; + public SearchResult Results; +} + +public readonly struct SearchResult(List results) +{ + public readonly List Results = results; +} \ No newline at end of file diff --git a/src/Server/Searchdomain.cs b/src/Server/Searchdomain.cs index 1df5dc7..602f2cc 100644 --- a/src/Server/Searchdomain.cs +++ b/src/Server/Searchdomain.cs @@ -3,6 +3,7 @@ using System.Data.Common; using ElmahCore.Mvc.Logger; using MySql.Data.MySqlClient; using Server.Helper; +using Server.Models; namespace Server; @@ -13,7 +14,7 @@ public class Searchdomain public AIProvider aIProvider; public string searchdomain; public int id; - public Dictionary)>> searchCache; // Yeah look at this abomination. searchCache[x][0] = last accessed time, searchCache[x][1] = results for x + public Dictionary searchCache; // Key: query, Value: Search results for that query (with timestamp) public List entityCache; public List modelsInUse; public Dictionary> embeddingCache; @@ -22,8 +23,6 @@ public class Searchdomain public SQLHelper helper; private readonly ILogger _logger; - // TODO Add settings and update cli/program.cs, as well as DatabaseInsertSearchdomain() - public Searchdomain(string searchdomain, string connectionString, AIProvider aIProvider, Dictionary> embeddingCache, ILogger logger, string provider = "sqlserver", bool runEmpty = false) { _connectionString = connectionString; @@ -47,6 +46,7 @@ public class Searchdomain public void UpdateEntityCache() { + InvalidateSearchCache(); Dictionary parametersIDSearchdomain = new() { ["id"] = this.id @@ -151,8 +151,14 @@ public class Searchdomain 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) { + if (searchCache.TryGetValue(query, out DateTimedSearchResult cachedResult)) + { + cachedResult.accessTimes.Add(DateTime.Now); + return [.. cachedResult.Results.Results.Select(r => (r.Score, r.Name))]; + } + if (!embeddingCache.TryGetValue(query, out Dictionary? queryEmbeddings)) { queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider); @@ -181,8 +187,17 @@ public class Searchdomain } result.Add((entity.probMethod(datapointProbs), entity.name)); } - - return [.. result.OrderByDescending(s => s.Item1)]; // [.. element] = element.ToList() + List<(float, string)> results = [.. result.OrderByDescending(s => s.Item1)]; + SearchResult searchResult = new( + [.. results.Select(r => + new ResultItem(r.Item1, r.Item2 ))] + ); + searchCache[query] = new DateTimedSearchResult + { + accessTimes = [DateTime.Now], + Results = searchResult + }; + return results; } public static List GetModels(List entities) @@ -217,4 +232,9 @@ public class Searchdomain reader.Close(); return this.id; } + + public void InvalidateSearchCache() + { + searchCache = []; + } } diff --git a/src/Server/SearchdomainManager.cs b/src/Server/SearchdomainManager.cs index 855cac7..e354f97 100644 --- a/src/Server/SearchdomainManager.cs +++ b/src/Server/SearchdomainManager.cs @@ -64,7 +64,9 @@ public class SearchdomainManager public void InvalidateSearchdomainCache(string searchdomainName) { - GetSearchdomain(searchdomainName).UpdateEntityCache(); + var searchdomain = GetSearchdomain(searchdomainName); + searchdomain.UpdateEntityCache(); + searchdomain.InvalidateSearchCache(); // TODO implement cache remediation (Suggestion: searchdomain-wide setting for cache remediation / invalidation - ) } public List ListSearchdomains()