Skip to content

Commit a2c260a

Browse files
committed
Fixed static cache reset
1 parent 2b8af17 commit a2c260a

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

LinkDotNet.Blog.Infrastructure/Persistence/CachedRepository.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@
99

1010
namespace LinkDotNet.Blog.Infrastructure.Persistence;
1111

12-
public class CachedRepository<T> : IRepository<T>
12+
public sealed class CachedRepository<T> : IRepository<T>, IDisposable
1313
where T : Entity
1414
{
15-
private static CancellationTokenSource resetToken = new();
16-
1715
private readonly IRepository<T> repository;
18-
1916
private readonly IMemoryCache memoryCache;
17+
private CancellationTokenSource resetToken = new();
2018

2119
public CachedRepository(IRepository<T> repository, IMemoryCache memoryCache)
2220
{
2321
this.repository = repository;
2422
this.memoryCache = memoryCache;
2523
}
2624

27-
private static MemoryCacheEntryOptions Options => new()
25+
private MemoryCacheEntryOptions Options => new()
2826
{
2927
ExpirationTokens = { new CancellationChangeToken(resetToken.Token) },
3028
AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(7),
@@ -70,7 +68,12 @@ public async Task DeleteAsync(string id)
7068
await repository.DeleteAsync(id);
7169
}
7270

73-
private static void ResetCache()
71+
public void Dispose()
72+
{
73+
resetToken?.Dispose();
74+
}
75+
76+
private void ResetCache()
7477
{
7578
if (resetToken is { IsCancellationRequested: false, Token: { CanBeCanceled: true } })
7679
{
@@ -80,4 +83,4 @@ private static void ResetCache()
8083

8184
resetToken = new CancellationTokenSource();
8285
}
83-
}
86+
}

LinkDotNet.Blog.UnitTests/Infrastructure/Persistence/CachedRepositoryTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace LinkDotNet.Blog.UnitTests.Infrastructure.Persistence;
1414

15-
public class CachedRepositoryTests
15+
public sealed class CachedRepositoryTests : IDisposable
1616
{
1717
private readonly Mock<IRepository<BlogPost>> repositoryMock;
1818
private readonly CachedRepository<BlogPost> sut;
@@ -140,6 +140,11 @@ public async Task ShouldGetFreshDataAfterDelete()
140140
Times.Exactly(2));
141141
}
142142

143+
public void Dispose()
144+
{
145+
sut.Dispose();
146+
}
147+
143148
private void SetupRepository()
144149
{
145150
var blogPost = new BlogPostBuilder().Build();

stylecop.analyzers.ruleset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
<Rule Id="S3898" Action="None" />
368368
<Rule Id="S3900" Action="None" />
369369
<Rule Id="S3902" Action="None" />
370-
<Rule Id="S3903" Action="Info" />
370+
<Rule Id="S3903" Action="None" />
371371
<Rule Id="S3904" Action="Info" />
372372
<Rule Id="S3906" Action="None" />
373373
<Rule Id="S3908" Action="None" />

0 commit comments

Comments
 (0)