Skip to content

Commit

Permalink
Adds a task to purge cache
Browse files Browse the repository at this point in the history
  • Loading branch information
scampower3 committed Mar 4, 2024
1 parent 3ffbb0b commit df4fec4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Jellyfin.Plugin.Tvdb/ScheduledTasks/PurgeCacheTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.Tvdb.ScheduledTasks
{
/// <summary>
/// Task to purge TheTVDB plugin cache.
/// </summary>
public class PurgeCacheTask : IScheduledTask
{
private readonly ILogger<PurgeCacheTask> _logger;
private readonly TvdbClientManager _tvdbClientManager;

/// <summary>
/// Initializes a new instance of the <see cref="PurgeCacheTask"/> class.
/// </summary>
/// <param name="logger">Instance of the <see cref="ILogger{TvdbScheduledTask}"/> interface.</param>
/// <param name="tvdbClientManager">Instance of <see cref="TvdbClientManager"/>.</param>
public PurgeCacheTask(
ILogger<PurgeCacheTask> logger,
TvdbClientManager tvdbClientManager)
{
_logger = logger;
_tvdbClientManager = tvdbClientManager;
}

/// <inheritdoc/>
public string Name => "Purge TheTVDB plugin cache";

/// <inheritdoc/>
public string Key => "PurgeTheTVDBPluginCache";

/// <inheritdoc/>
public string Description => "Purges the TheTVDB Cache";

/// <inheritdoc/>
public string Category => "TheTVDB";

/// <inheritdoc/>
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
if (_tvdbClientManager.PurgeCache())
{
_logger.LogInformation("TheTvdb plugin cache purged successfully");
}
else
{
_logger.LogError("TheTvdb plugin cache purge failed");
}

return Task.CompletedTask;
}

/// <inheritdoc/>
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return Enumerable.Empty<TaskTriggerInfo>();
}
}
}
17 changes: 17 additions & 0 deletions Jellyfin.Plugin.Tvdb/TvdbClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,23 @@ public async Task<IReadOnlyList<ArtworkType>> GetArtworkTypeAsync(CancellationTo
}
}

/// <summary>
/// Purge the cache.
/// </summary>
/// <returns>True if success else false.</returns>
public bool PurgeCache()
{
if (_memoryCache is MemoryCache memoryCache)
{
memoryCache.Compact(1);
return true;
}
else
{
return false;
}
}

/// <summary>
/// Create an independent ServiceProvider because registering HttpClients directly into Jellyfin
/// causes issues upstream.
Expand Down

0 comments on commit df4fec4

Please sign in to comment.