Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <summary>
/// Represents a manager for memory caching
/// </summary>
public class MemoryCacheBase : ICacheBase, IDisposable

Check warning on line 13 in src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Fix this implementation of 'IDisposable' to conform to the dispose pattern.
{
#region Ctor

Expand Down Expand Up @@ -46,7 +46,7 @@
{
if (_cache.TryGetValue(key, out T cacheEntry)) return cacheEntry;
var semaphore = CacheEntries.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
semaphore.Wait();
semaphore.Wait(CancellationToken.None);
try
Comment thread
KrzysztofPajak marked this conversation as resolved.
{
if (!_cache.TryGetValue(key, out cacheEntry))
Expand All @@ -72,7 +72,7 @@
{
if (_cache.TryGetValue(key, out T cacheEntry)) return cacheEntry;
var semaphore = CacheEntries.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
await semaphore.WaitAsync();

Check warning on line 75 in src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Pass the 'this._resetCacheToken.Token' to this method to allow cancellation of the operation, or use 'CancellationToken.None' to opt out explicitly.
try
{
if (!_cache.TryGetValue(key, out cacheEntry))
Expand All @@ -97,7 +97,7 @@
public virtual async Task<T> SetAsync<T>(string key, Func<Task<T>> acquire, int cacheTime)
{
var semaphore = CacheEntries.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
await semaphore.WaitAsync();

Check warning on line 100 in src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Pass the 'this._resetCacheToken.Token' to this method to allow cancellation of the operation, or use 'CancellationToken.None' to opt out explicitly.
try
{
var cacheEntry = await acquire();
Expand All @@ -115,7 +115,7 @@
_cache.Remove(key);

if (publisher)
await _mediator.Publish(new EntityCacheEvent(key, CacheEvent.RemoveKey));

Check warning on line 118 in src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Pass the 'this._resetCacheToken.Token' to this method to allow cancellation of the operation, or use 'CancellationToken.None' to opt out explicitly.
}

public virtual async Task RemoveByPrefix(string prefix, bool publisher = true)
Expand All @@ -124,7 +124,7 @@
foreach (var cacheEntries in entriesToRemove) _cache.Remove(cacheEntries.Key);

if (publisher)
await _mediator.Publish(new EntityCacheEvent(prefix, CacheEvent.RemovePrefix));

Check warning on line 127 in src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs

View workflow job for this annotation

GitHub Actions / Build and analyze

Pass the 'this._resetCacheToken.Token' to this method to allow cancellation of the operation, or use 'CancellationToken.None' to opt out explicitly.
}

public virtual Task Clear(bool publisher = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@
Assert.AreEqual("test", result);
}

[TestMethod]
[Timeout(10000)]

Check warning on line 48 in src/Tests/Grand.Infrastructure.Tests/Caching/MemoryCacheBaseTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'CooperativeCancellation = true' with '[Timeout]' to enable cooperative cancellation behavior

See more on https://sonarcloud.io/project/issues?id=grandnode_grandnode2&issues=AaB3A_0N3pc0oZUN4llX&open=AaB3A_0N3pc0oZUN4llX&pullRequest=818
public async Task Get_WhenClearCancelsWaitingSemaphore_DoesNotThrow()
{
var acquireStarted = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var releaseAcquire = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

var firstRead = _service.GetAsync("key", async () =>
{
acquireStarted.SetResult(true);
await releaseAcquire.Task;
return "test";
}, 1);

await acquireStarted.Task;

var secondRead = Task.Run(() => _service.Get("key", () => "fallback", 1));

_service.Clear(false).GetAwaiter().GetResult();
releaseAcquire.SetResult(true);

Assert.AreEqual("test", await firstRead);
Assert.AreEqual("test", await secondRead);
}

[TestMethod]
public async Task GetAsyncTest()
{
Expand Down
Loading