Skip to content

Commit bdffd18

Browse files
committed
double result implementation.
1 parent e3b6930 commit bdffd18

4 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/MultiCache.StackExchangeRedis/MultiCache.StackExchangeRedis.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
3030
</ItemGroup>
3131
<ItemGroup>
32-
<PackageReference Include="MultiCache" Version="1.0.1" />
32+
<PackageReference Include="MultiCache" Version="1.0.2" />
3333
</ItemGroup>
3434
</Project>

src/MultiCache.StackExchangeRedis/Redis/RedisMultiCacheManager.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ public RedisMultiCacheManager(IRedisClient redisClient, IMemoryClient memoryCach
1818
_memoryCache = memoryCache;
1919
}
2020

21-
public async Task<T> GetAsync<T>(string key, CancellationToken cancellationToken = default)
21+
public async Task<(T, bool)> GetAsync<T>(string key, CancellationToken cancellationToken = default)
2222
{
2323
cancellationToken.ThrowIfCancellationRequested();
2424

2525
var isExistsInMemory = _memoryCache.TryGetValue(key, out T response);
2626
if (isExistsInMemory)
2727
{
28-
return response;
28+
return (response, true);
2929
}
3030

3131
var (value, expiry) = await _redisClient.GetWithExpiryAsync(key, cancellationToken).ConfigureAwait(false);
3232
if (value == null)
3333
{
34-
return default;
34+
return (default, false);
3535
}
3636

3737
response = JsonSerializer.Deserialize<T>(value);
@@ -44,7 +44,7 @@ public async Task<T> GetAsync<T>(string key, CancellationToken cancellationToken
4444
_memoryCache.Set(key, response);
4545
}
4646

47-
return response;
47+
return (response, true);
4848
}
4949

5050
public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null, CancellationToken cancellationToken = default)
@@ -63,30 +63,30 @@ public async Task SetAsync<T>(string key, T value, TimeSpan? expiry = null, Canc
6363
await _redisClient.SetAsync(key, JsonSerializer.SerializeToUtf8Bytes(value), expiry, cancellationToken).ConfigureAwait(false);
6464
}
6565

66-
public async Task<T> GetOrCreateAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiry = null, CancellationToken cancellationToken = default)
66+
public async Task<(T, bool)> GetOrCreateAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiry = null, CancellationToken cancellationToken = default)
6767
{
6868
cancellationToken.ThrowIfCancellationRequested();
6969

70-
var cacheResult = await GetAsync<T>(key, cancellationToken);
70+
var (cacheResult, fromCache) = await GetAsync<T>(key, cancellationToken);
7171
if (cacheResult != null)
72-
return cacheResult;
72+
return (cacheResult, fromCache);
7373

7474
var result = await factory();
7575

7676
if (result == null)
7777
{
78-
return default(T);
78+
return (default(T), false);
7979
}
8080

8181
await SetAsync(key, result, expiry, cancellationToken);
82-
return result;
82+
return (result, true);
8383
}
8484

8585
public async Task RemoveAsync(string key, CancellationToken cancellationToken)
8686
{
8787
cancellationToken.ThrowIfCancellationRequested();
88-
88+
8989
await _redisClient.RemoveAsync(key, cancellationToken);
9090
}
9191
}
92-
}
92+
}

tests/MultiCache.StackExchangeRedis.Tests/MultiCacheManagerTests/GetAsyncTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public async Task Should_Return_From_Memory_If_Cache_Exist_In_Memory(string cach
4444
_memoryClientMock.Setup(q => q.TryGetValue(cacheKey, out response)).Returns(true);
4545

4646
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
47-
var cachedResult = await multiCacheManager.GetAsync<string>(cacheKey);
47+
var (cachedResult, fromCache) = await multiCacheManager.GetAsync<string>(cacheKey);
4848

4949
Assert.Equal(response, cachedResult);
50+
Assert.True(fromCache);
5051
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
5152
}
5253

@@ -58,10 +59,10 @@ public async Task Should_Return_Empty_If_Cache_Doesnt_Exist_In_Memory_And_Redis(
5859
_redisClientMock.Setup(q => q.GetWithExpiryAsync(cacheKey, default)).ReturnsAsync(() => (null, TimeSpan.Zero));
5960

6061
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
61-
var cachedResult = await multiCacheManager.GetAsync<string>(cacheKey);
62+
var (cachedResult, fromCache) = await multiCacheManager.GetAsync<string>(cacheKey);
6263

6364
Assert.Equal(default, cachedResult);
64-
65+
Assert.False(fromCache);
6566
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
6667
_redisClientMock.Verify(q => q.GetWithExpiryAsync(cacheKey, It.IsAny<CancellationToken>()), Times.Once);
6768
}
@@ -77,9 +78,10 @@ public async Task Should_Return_From_Redis_And_Set_Memory_With_TTL_When_Cache_Ex
7778
_redisClientMock.Setup(q => q.GetWithExpiryAsync(cacheKey, default)).ReturnsAsync(() => (JsonSerializer.SerializeToUtf8Bytes(response), redisTtl));
7879

7980
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
80-
var cachedResult = await multiCacheManager.GetAsync<string>(cacheKey);
81+
var (cachedResult, fromCache) = await multiCacheManager.GetAsync<string>(cacheKey);
8182

8283
Assert.Equal(response, cachedResult);
84+
Assert.True(fromCache);
8385
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
8486
_memoryClientMock.Verify(q => q.Set(cacheKey, response, redisTtl), Times.Once);
8587
_redisClientMock.Verify(q => q.GetWithExpiryAsync(cacheKey, It.IsAny<CancellationToken>()), Times.Once);
@@ -95,12 +97,13 @@ public async Task Should_Return_From_Redis_And_Set_Memory_Without_TTL_When_Cache
9597
_redisClientMock.Setup(q => q.GetWithExpiryAsync(cacheKey, default)).ReturnsAsync(() => (JsonSerializer.SerializeToUtf8Bytes(response), null));
9698

9799
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
98-
var cachedResult = await multiCacheManager.GetAsync<string>(cacheKey);
100+
var (cachedResult, fromCache) = await multiCacheManager.GetAsync<string>(cacheKey);
99101

100102
Assert.Equal(response, cachedResult);
103+
Assert.True(fromCache);
101104
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
102105
_memoryClientMock.Verify(q => q.Set(cacheKey, response), Times.Once);
103106
_redisClientMock.Verify(q => q.GetWithExpiryAsync(cacheKey, It.IsAny<CancellationToken>()), Times.Once);
104107
}
105108
}
106-
}
109+
}

tests/MultiCache.StackExchangeRedis.Tests/MultiCacheManagerTests/GetOrCreateAsyncTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public async Task Should_Return_Data_When_Cache_Exist_In_Cache(string cacheKey)
4343
_memoryClientMock.Setup(q => q.TryGetValue(cacheKey, out response)).Returns(true);
4444

4545
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
46-
var cachedResult = await multiCacheManager.GetOrCreateAsync(cacheKey, async () => await Task.FromResult(Guid.NewGuid().ToString()), TimeSpan.Zero);
47-
46+
var (cachedResult,fromCache) = await multiCacheManager.GetOrCreateAsync(cacheKey, async () => await Task.FromResult(Guid.NewGuid().ToString()), TimeSpan.Zero);
4847
Assert.Equal(response, cachedResult);
48+
Assert.True(fromCache);
4949
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
5050
}
5151

@@ -62,9 +62,10 @@ static Task<string> Func()
6262
}
6363

6464
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
65-
var cachedResult = await multiCacheManager.GetOrCreateAsync(cacheKey, Func, TimeSpan.Zero);
65+
var (cachedResult,fromCache) = await multiCacheManager.GetOrCreateAsync(cacheKey, Func, TimeSpan.Zero);
6666

6767
Assert.Null(cachedResult);
68+
Assert.False(fromCache);
6869
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
6970
_redisClientMock.Verify(q => q.GetWithExpiryAsync(cacheKey, It.IsAny<CancellationToken>()), Times.Once);
7071
}
@@ -88,9 +89,10 @@ Task<string> Func()
8889
}
8990

9091
var multiCacheManager = _autoMock.Create<RedisMultiCacheManager>();
91-
var cachedResult = await multiCacheManager.GetOrCreateAsync(cacheKey, Func, timeSpan);
92+
var (cachedResult,fromCache) = await multiCacheManager.GetOrCreateAsync(cacheKey, Func, timeSpan);
9293

9394
Assert.Equal(response, cachedResult);
95+
Assert.True(fromCache);
9496
_memoryClientMock.Verify(q => q.TryGetValue(cacheKey, out response), Times.Once);
9597
_memoryClientMock.Verify(q => q.Set(cacheKey, response, timeSpan), Times.Once);
9698
_redisClientMock.Verify(q => q.GetWithExpiryAsync(cacheKey, It.IsAny<CancellationToken>()), Times.Once);

0 commit comments

Comments
 (0)