Skip to content

Commit

Permalink
Merge pull request #120 from scampower3/cache-configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero authored Mar 10, 2024
2 parents 09249e1 + 346df57 commit 2f03b60
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
20 changes: 20 additions & 0 deletions Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class PluginConfiguration : BasePluginConfiguration
/// Gets the tvdb api key for project.
/// </summary>
public const string ProjectApiKey = "7f7eed88-2530-4f84-8ee7-f154471b8f87";
private int _cacheDurationInHours = 1;
private int _cacheDurationInDays = 7;

/// <summary>
/// Gets or sets the tvdb api key for user.
Expand All @@ -19,5 +21,23 @@ public class PluginConfiguration : BasePluginConfiguration
/// This is the subscriber's pin.
/// </remarks>
public string ApiKey { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the cache in hours.
/// </summary>
public int CacheDurationInHours
{
get => _cacheDurationInHours;
set => _cacheDurationInHours = value < 1 ? 1 : value;
}

/// <summary>
/// Gets or sets the cache in days.
/// </summary>
public int CacheDurationInDays
{
get => _cacheDurationInDays;
set => _cacheDurationInDays = value < 1 ? 7 : value;
}
}
}
23 changes: 20 additions & 3 deletions Jellyfin.Plugin.Tvdb/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@ <h2 class="sectionTitle">TheTVDB Settings:</h2>
</div>
<div class="inputContainer">
<label for="txtTvdbApiKey">TheTvdb API Key:</label>
<input type="text" id="txtTvdbApiKey" required="required" is="emby-input"/>
<input type="text" id="txtTvdbApiKey" required="required" is="emby-input" />
<div class="fieldDescription">
TheTVDB Api Key from Subscriptions.
</div>
</div>
<br/>
<div class="inputContainer">
<label for="txtTvdbApiKey">Cache time in hours:</label>
<input type="number" id="cacheDurationInHours" required="required" is="emby-input" />
<div class="fieldDescription">
The cache time in hours for metadata.
</div>
</div>
<div class="inputContainer">
<label for="txtTvdbApiKey">Cache time in days:</label>
<input type="number" id="cacheDurationInDays" required="required" is="emby-input" />
<div class="fieldDescription">
The cache time in days for Languages and Activity Type metadata.
</div>
</div>
<br />
<div>
<button is="emby-button" type="submit" data-theme="b" class="raised button-submit block">
<span>Save</span>
Expand All @@ -40,6 +54,8 @@ <h2 class="sectionTitle">TheTVDB Settings:</h2>

ApiClient.getPluginConfiguration(TvdbPluginConfiguration.uniquePluginId).then(function (config) {
document.getElementById('txtTvdbApiKey').value = config.ApiKey || '';
document.getElementById('cacheDurationInHours').value = config.CacheDurationInHours;
document.getElementById('cacheDurationInDays').value = config.CacheDurationInDays;
Dashboard.hideLoadingMsg();
});
},
Expand All @@ -49,7 +65,8 @@ <h2 class="sectionTitle">TheTVDB Settings:</h2>

ApiClient.getPluginConfiguration(TvdbPluginConfiguration.uniquePluginId).then(function (config) {
config.ApiKey = document.getElementById('txtTvdbApiKey').value;

config.CacheDurationInHours = document.getElementById('cacheDurationInHours').value;
config.CacheDurationInDays = document.getElementById('cacheDurationInDays').value;
ApiClient.updatePluginConfiguration(TvdbPluginConfiguration.uniquePluginId, config).then(function (result) {
Dashboard.processPluginConfigurationUpdateResult(result);
});
Expand Down
9 changes: 6 additions & 3 deletions Jellyfin.Plugin.Tvdb/TvdbClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Jellyfin.Plugin.Tvdb;
public class TvdbClientManager : IDisposable
{
private const string TvdbHttpClient = "TvdbHttpClient";
private const int CacheDurationInHours = 1;
private static readonly SemaphoreSlim _tokenUpdateLock = new SemaphoreSlim(1, 1);

private readonly IHttpClientFactory _httpClientFactory;
Expand All @@ -48,6 +47,10 @@ public TvdbClientManager(IApplicationHost applicationHost)

private static string? UserPin => TvdbPlugin.Instance?.Configuration.ApiKey;

private static int CacheDurationInHours => TvdbPlugin.Instance?.Configuration.CacheDurationInHours ?? 1;

private static int CacheDurationInDays => TvdbPlugin.Instance?.Configuration.CacheDurationInDays ?? 7;

/// <summary>
/// Logs in or refresh login to the tvdb api when needed.
/// </summary>
Expand Down Expand Up @@ -370,7 +373,7 @@ public async Task<IReadOnlyList<Language>> GetLanguagesAsync(CancellationToken c
await LoginAsync().ConfigureAwait(false);
var languagesResult = await languagesClient.GetAllLanguagesAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
_memoryCache.Set(key, languagesResult.Data, TimeSpan.FromDays(1));
_memoryCache.Set(key, languagesResult.Data, TimeSpan.FromDays(CacheDurationInDays));
return languagesResult.Data;
}

Expand All @@ -391,7 +394,7 @@ public async Task<IReadOnlyList<ArtworkType>> GetArtworkTypeAsync(CancellationTo
await LoginAsync().ConfigureAwait(false);
var artworkTypesResult = await artworkTypesClient.GetAllArtworkTypesAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
_memoryCache.Set(key, artworkTypesResult.Data, TimeSpan.FromDays(1));
_memoryCache.Set(key, artworkTypesResult.Data, TimeSpan.FromDays(CacheDurationInDays));
return artworkTypesResult.Data;
}

Expand Down

0 comments on commit 2f03b60

Please sign in to comment.