158 lines
5.9 KiB
C#
158 lines
5.9 KiB
C#
using Microsoft.AspNetCore.Mvc.Formatters;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
using System.Text.RegularExpressions;
|
|
using UmlautAdaptarr.Models;
|
|
using UmlautAdaptarr.Utilities;
|
|
|
|
namespace UmlautAdaptarr.Services
|
|
{
|
|
public partial class CacheService(IMemoryCache cache)
|
|
{
|
|
private readonly Dictionary<string, HashSet<string>> VariationIndex = [];
|
|
private readonly Dictionary<string, List<(HashSet<string> TitleVariations, string CacheKey)>> AudioVariationIndex = [];
|
|
private const int VARIATION_LOOKUP_CACHE_LENGTH = 5;
|
|
|
|
public void CacheSearchItem(SearchItem item)
|
|
{
|
|
var prefix = item.MediaType;
|
|
var cacheKey = $"{prefix}_extid_{item.ExternalId}";
|
|
cache.Set(cacheKey, item);
|
|
if (item.MediaType == "audio")
|
|
{
|
|
CacheAudioSearchItem(item, cacheKey);
|
|
return;
|
|
}
|
|
|
|
var normalizedTitle = item.Title.RemoveAccentButKeepGermanUmlauts().ToLower();
|
|
|
|
cache.Set($"{prefix}_title_{normalizedTitle}", item);
|
|
|
|
foreach (var variation in item.TitleMatchVariations)
|
|
{
|
|
var normalizedVariation = variation.RemoveAccentButKeepGermanUmlauts().ToLower();
|
|
cacheKey = $"{prefix}_var_{normalizedVariation}";
|
|
cache.Set(cacheKey, item);
|
|
|
|
// Indexing by prefix
|
|
var indexPrefix = normalizedVariation[..Math.Min(VARIATION_LOOKUP_CACHE_LENGTH, variation.Length)].ToLower();
|
|
if (!VariationIndex.ContainsKey(indexPrefix))
|
|
{
|
|
VariationIndex[indexPrefix] = [];
|
|
}
|
|
VariationIndex[indexPrefix].Add(cacheKey);
|
|
}
|
|
}
|
|
|
|
public void CacheAudioSearchItem(SearchItem item, string cacheKey)
|
|
{
|
|
// Index author and title variations
|
|
foreach (var authorVariation in item.AuthorMatchVariations)
|
|
{
|
|
var normalizedAuthor = authorVariation.NormalizeForComparison();
|
|
|
|
if (!AudioVariationIndex.ContainsKey(normalizedAuthor))
|
|
{
|
|
AudioVariationIndex[normalizedAuthor] = [];
|
|
}
|
|
|
|
var titleVariations = item.TitleMatchVariations.Select(titleMatchVariation => titleMatchVariation.NormalizeForComparison()).ToHashSet();
|
|
AudioVariationIndex[normalizedAuthor].Add((titleVariations, cacheKey));
|
|
}
|
|
}
|
|
|
|
public SearchItem? SearchItemByTitle(string mediaType, string title)
|
|
{
|
|
var normalizedTitle = title.RemoveAccentButKeepGermanUmlauts().ToLower();
|
|
|
|
if (mediaType == "audio")
|
|
{
|
|
return FindBestMatchForAudio(normalizedTitle.NormalizeForComparison());
|
|
}
|
|
|
|
// Use the first few characters of the normalized title for cache prefix search
|
|
var cacheSearchPrefix = normalizedTitle[..Math.Min(VARIATION_LOOKUP_CACHE_LENGTH, normalizedTitle.Length)];
|
|
|
|
if (VariationIndex.TryGetValue(cacheSearchPrefix, out var cacheKeys))
|
|
{
|
|
foreach (var cacheKey in cacheKeys)
|
|
{
|
|
if (cache.TryGetValue(cacheKey, out SearchItem? item))
|
|
{
|
|
if (item?.MediaType != mediaType)
|
|
{
|
|
continue;
|
|
}
|
|
// After finding a potential item, compare normalizedTitle with each German title variation
|
|
foreach (var variation in item?.TitleMatchVariations ?? [])
|
|
{
|
|
var normalizedVariation = variation.RemoveAccentButKeepGermanUmlauts().ToLower();
|
|
if (normalizedTitle.StartsWith(variation, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return item;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public SearchItem? GetSearchItemByExternalId(string mediaType, string externalId)
|
|
{
|
|
if (cache.TryGetValue($"{mediaType}_extid_{externalId}", out SearchItem? item))
|
|
{
|
|
return item;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public SearchItem? GetSearchItemByTitle(string mediaType, string title)
|
|
{
|
|
var normalizedTitle = title.RemoveAccentButKeepGermanUmlauts().ToLower();
|
|
|
|
|
|
if (mediaType == "generic")
|
|
{
|
|
// TODO
|
|
}
|
|
|
|
cache.TryGetValue($"{mediaType}_var_{normalizedTitle}", out SearchItem? item);
|
|
if (item == null)
|
|
{
|
|
cache.TryGetValue($"{mediaType}_title_{normalizedTitle}", out item);
|
|
}
|
|
return item;
|
|
}
|
|
|
|
private SearchItem? FindBestMatchForAudio(string normalizedOriginalTitle)
|
|
{
|
|
foreach (var authorEntry in AudioVariationIndex)
|
|
{
|
|
if (normalizedOriginalTitle.Contains(authorEntry.Key))
|
|
{
|
|
var sortedEntries = authorEntry.Value.OrderByDescending(entry => entry.TitleVariations.FirstOrDefault()?.Length).ToList();
|
|
|
|
foreach (var (titleVariations, cacheKey) in sortedEntries)
|
|
{
|
|
if (titleVariations.Any(normalizedOriginalTitle.Contains))
|
|
{
|
|
if (cache.TryGetValue(cacheKey, out SearchItem? item))
|
|
{
|
|
return item;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
[GeneratedRegex("\\s")]
|
|
private static partial Regex WhiteSpaceRegex();
|
|
}
|
|
}
|