Skip to content

Commit

Permalink
Merge pull request #123 from scampower3/cache-fix
Browse files Browse the repository at this point in the history
Fixes some cache key clashes and other improvements.
  • Loading branch information
Shadowghost authored Mar 12, 2024
2 parents e5f3d4d + 45eea79 commit 9dd7a7d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ private async Task<IEnumerable<RemoteSearchResult>> FetchSeriesSearchResult(Seri
{
var seriesResult =
await _tvdbClientManager
.GetSeriesExtendedByIdAsync(tvdbId.Value, seriesInfo.MetadataLanguage, cancellationToken, small: true)
.GetSeriesExtendedByIdAsync(tvdbId.Value, seriesInfo.MetadataLanguage, cancellationToken, meta: Meta4.Translations, small: true)
.ConfigureAwait(false);
return new[] { MapSeriesToRemoteSearchResult(seriesResult) };
return new[] { MapSeriesToRemoteSearchResult(seriesResult, seriesInfo.MetadataLanguage) };
}
catch (Exception e)
{
Expand All @@ -146,12 +146,12 @@ await _tvdbClientManager
}
}

private RemoteSearchResult MapSeriesToRemoteSearchResult(SeriesExtendedRecord series)
private RemoteSearchResult MapSeriesToRemoteSearchResult(SeriesExtendedRecord series, string language)
{
var remoteResult = new RemoteSearchResult
{
Name = series.Name,
Overview = series.Overview?.Trim() ?? string.Empty,
Name = series.Translations.GetTranslatedNamedOrDefault(language) ?? series.Name,
Overview = series.Translations.GetTranslatedOverviewOrDefault(language)?.Trim() ?? series.Overview?.Trim(),
SearchProviderName = Name,
ImageUrl = series.Image
};
Expand Down Expand Up @@ -317,7 +317,7 @@ private async Task<List<RemoteSearchResult>> FindSeriesInternal(string name, str
{
var tvdbTitles = new List<string>
{
seriesSearchResult.Name
seriesSearchResult.Translations.GetTranslatedNamedOrDefault(language) ?? seriesSearchResult.Name
};
if (seriesSearchResult.Aliases is not null)
{
Expand Down
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.Tvdb/TvdbClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public async Task<SeriesExtendedRecord> GetSeriesExtendedByIdAsync(
Meta4? meta = null,
bool? small = null)
{
var key = $"TvdbSeriesExtended_{tvdbId.ToString(CultureInfo.InvariantCulture)}";
var key = $"TvdbSeriesExtended_{tvdbId.ToString(CultureInfo.InvariantCulture)}_{meta}_{small}";
if (_memoryCache.TryGetValue(key, out SeriesExtendedRecord series))
{
return series;
Expand Down Expand Up @@ -186,7 +186,7 @@ public async Task<Data2> GetSeriesEpisodesAsync(
string seasonType,
CancellationToken cancellationToken)
{
var key = $"TvdbSeriesEpisodes_{tvdbId.ToString(CultureInfo.InvariantCulture)}";
var key = $"TvdbSeriesEpisodes_{tvdbId.ToString(CultureInfo.InvariantCulture)}_{seasonType}";
if (_memoryCache.TryGetValue(key, out Data2 series))
{
return series;
Expand Down
21 changes: 17 additions & 4 deletions Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,23 @@ public static class TvdbSdkExtensions
{
return translations?
.NameTranslations?
.FirstOrDefault(translation => IsMatch(translation, language))?
.FirstOrDefault(translation => IsMatch(translation.Language, language))?
.Name;
}

/// <summary>
/// Get the translated Name, or <see langword="null"/>.
/// </summary>
/// <param name="translations">Available translations.</param>
/// <param name="language">Requested language.</param>
/// <returns>Translated Name, or <see langword="null"/>.</returns>
public static string? GetTranslatedNamedOrDefault(this TranslationSimple? translations, string? language)
{
return translations?
.FirstOrDefault(translation => IsMatch(translation.Key, language))
.Value;
}

/// <summary>
/// Get the translated Overview, or <see langword="null"/>.
/// </summary>
Expand All @@ -41,11 +54,11 @@ public static class TvdbSdkExtensions
{
return translations?
.OverviewTranslations?
.FirstOrDefault(translation => IsMatch(translation, language))?
.FirstOrDefault(translation => IsMatch(translation.Language, language))?
.Overview;
}

private static bool IsMatch(this Translation translation, string? language)
private static bool IsMatch(this string translation, string? language)
{
if (string.IsNullOrWhiteSpace(language))
{
Expand All @@ -62,7 +75,7 @@ private static bool IsMatch(this Translation translation, string? language)
// try to find a match (ISO 639-2)
return TvdbCultureInfo.GetCultureInfo(language!)?
.ThreeLetterISOLanguageNames?
.Contains(translation.Language, StringComparer.OrdinalIgnoreCase)
.Contains(translation, StringComparer.OrdinalIgnoreCase)
?? false;
}

Expand Down

0 comments on commit 9dd7a7d

Please sign in to comment.