Skip to content

Commit 3dcf3c2

Browse files
authored
Allow expiration removal loop to be disabled (#5)
* Use a single thread loop to delete expired items * Rollback minimum cache loop to 5 minutes * Clean up code * Not exposing class * Using cancellation tokens * Split responsibilities into each class Clean up code * Allow expiration removal loop to be disabled * Formatting
1 parent 4baa2d1 commit 3dcf3c2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Extensions.Caching.PostgreSql/DatabaseExpiredItemsRemoverLoop.cs

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal sealed class DatabaseExpiredItemsRemoverLoop : IDatabaseExpiredItemsRem
1616
private readonly IDatabaseOperations _databaseOperations;
1717
private readonly CancellationTokenSource _cancellationTokenSource;
1818
private readonly ISystemClock _systemClock;
19+
private readonly bool _disabled;
1920

2021
public DatabaseExpiredItemsRemoverLoop(
2122
IOptions<PostgreSqlCacheOptions> options,
@@ -24,6 +25,12 @@ public DatabaseExpiredItemsRemoverLoop(
2425
{
2526
var cacheOptions = options.Value;
2627

28+
if ((_disabled = cacheOptions.Disabled) == true)
29+
{
30+
//No need to configure anything
31+
return;
32+
}
33+
2734
if (cacheOptions.ExpiredItemsDeletionInterval.HasValue &&
2835
cacheOptions.ExpiredItemsDeletionInterval.Value < MinimumExpiredItemsDeletionInterval)
2936
{
@@ -41,6 +48,11 @@ public DatabaseExpiredItemsRemoverLoop(
4148

4249
public void Start()
4350
{
51+
if (_disabled)
52+
{
53+
return;
54+
}
55+
4456
Task.Run(DeleteExpiredCacheItems);
4557
}
4658

Extensions.Caching.PostgreSql/PostGreSqlCacheOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class PostgreSqlCacheOptions : IOptions<PostgreSqlCacheOptions>
4242
/// </summary>
4343
public TimeSpan DefaultSlidingExpiration { get; set; } = TimeSpan.FromMinutes(20);
4444

45+
/// <summary>
46+
/// If set to true this instance of the cache will not remove expired items.
47+
/// </summary>
48+
public bool Disabled { get; set; }
49+
4550
PostgreSqlCacheOptions IOptions<PostgreSqlCacheOptions>.Value => this;
4651
}
4752
}

0 commit comments

Comments
 (0)