Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 1daefb6

Browse files
committed
reapply using IAsyncDisposable
1 parent 602cfe5 commit 1daefb6

22 files changed

+476
-728
lines changed

src/ServiceStack.Redis/BasicRedisClientManager.Async.cs

Lines changed: 43 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using ServiceStack.Redis.Internal;
1515
using System;
1616
using System.Collections.Generic;
17+
using System.Runtime.CompilerServices;
1718
using System.Threading;
1819
using System.Threading.Tasks;
1920

@@ -55,182 +56,125 @@ ValueTask IAsyncDisposable.DisposeAsync()
5556

5657
async Task<T> ICacheClientAsync.GetAsync<T>(string key, CancellationToken cancellationToken)
5758
{
58-
var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
59-
await using (client as IAsyncDisposable)
60-
{
61-
return await client.GetAsync<T>(key).ConfigureAwait(false);
62-
}
59+
await using var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
60+
return await client.GetAsync<T>(key).ConfigureAwait(false);
6361
}
6462

6563
async Task<bool> ICacheClientAsync.SetAsync<T>(string key, T value, CancellationToken cancellationToken)
6664
{
67-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
68-
await using (client as IAsyncDisposable)
69-
{
70-
return await client.SetAsync<T>(key, value, cancellationToken).ConfigureAwait(false);
71-
}
65+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
66+
return await client.SetAsync<T>(key, value, cancellationToken).ConfigureAwait(false);
7267
}
7368

7469
async Task<bool> ICacheClientAsync.SetAsync<T>(string key, T value, DateTime expiresAt, CancellationToken cancellationToken)
7570
{
76-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
77-
await using (client as IAsyncDisposable)
78-
{
79-
return await client.SetAsync<T>(key, value, expiresAt, cancellationToken).ConfigureAwait(false);
80-
}
71+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
72+
return await client.SetAsync<T>(key, value, expiresAt, cancellationToken).ConfigureAwait(false);
8173
}
8274

8375
async Task<bool> ICacheClientAsync.SetAsync<T>(string key, T value, TimeSpan expiresIn, CancellationToken cancellationToken)
8476
{
85-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
86-
await using (client as IAsyncDisposable)
87-
{
88-
return await client.SetAsync<T>(key, value, expiresIn, cancellationToken).ConfigureAwait(false);
89-
}
77+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
78+
return await client.SetAsync<T>(key, value, expiresIn, cancellationToken).ConfigureAwait(false);
9079
}
9180

9281
async Task ICacheClientAsync.FlushAllAsync(CancellationToken cancellationToken)
9382
{
94-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
95-
await using (client as IAsyncDisposable)
96-
{
97-
await client.FlushAllAsync(cancellationToken).ConfigureAwait(false);
98-
}
83+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
84+
await client.FlushAllAsync(cancellationToken).ConfigureAwait(false);
9985
}
10086

10187
async Task<IDictionary<string, T>> ICacheClientAsync.GetAllAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken)
10288
{
103-
var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
104-
await using (client as IAsyncDisposable)
105-
{
106-
return await client.GetAllAsync<T>(keys, cancellationToken).ConfigureAwait(false);
107-
}
89+
await using var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
90+
return await client.GetAllAsync<T>(keys, cancellationToken).ConfigureAwait(false);
10891
}
10992

11093
async Task ICacheClientAsync.SetAllAsync<T>(IDictionary<string, T> values, CancellationToken cancellationToken)
11194
{
112-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
113-
await using (client as IAsyncDisposable)
114-
{
115-
await client.SetAllAsync<T>(values, cancellationToken).ConfigureAwait(false);
116-
}
95+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
96+
await client.SetAllAsync<T>(values, cancellationToken).ConfigureAwait(false);
11797
}
11898

11999
async Task<bool> ICacheClientAsync.RemoveAsync(string key, CancellationToken cancellationToken)
120100
{
121-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
122-
await using (client as IAsyncDisposable)
123-
{
124-
return await client.RemoveAsync(key, cancellationToken).ConfigureAwait(false);
125-
}
101+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
102+
return await client.RemoveAsync(key, cancellationToken).ConfigureAwait(false);
126103
}
127104

128105
async Task ICacheClientAsync.RemoveAllAsync(IEnumerable<string> keys, CancellationToken cancellationToken)
129106
{
130-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
131-
await using (client as IAsyncDisposable)
132-
{
133-
await client.RemoveAllAsync(keys, cancellationToken).ConfigureAwait(false);
134-
}
107+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
108+
await client.RemoveAllAsync(keys, cancellationToken).ConfigureAwait(false);
135109
}
136110

137111
async Task<long> ICacheClientAsync.IncrementAsync(string key, uint amount, CancellationToken cancellationToken)
138112
{
139-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
140-
await using (client as IAsyncDisposable)
141-
{
142-
return await client.IncrementAsync(key, amount, cancellationToken).ConfigureAwait(false);
143-
}
113+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
114+
return await client.IncrementAsync(key, amount, cancellationToken).ConfigureAwait(false);
144115
}
145116

146117
async Task<long> ICacheClientAsync.DecrementAsync(string key, uint amount, CancellationToken cancellationToken)
147118
{
148-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
149-
await using (client as IAsyncDisposable)
150-
{
151-
return await client.DecrementAsync(key, amount, cancellationToken).ConfigureAwait(false);
152-
}
119+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
120+
return await client.DecrementAsync(key, amount, cancellationToken).ConfigureAwait(false);
153121
}
154122

155123
async Task<bool> ICacheClientAsync.AddAsync<T>(string key, T value, CancellationToken cancellationToken)
156124
{
157-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
158-
await using (client as IAsyncDisposable)
159-
{
160-
return await client.AddAsync<T>(key, value, cancellationToken).ConfigureAwait(false);
161-
}
125+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
126+
return await client.AddAsync<T>(key, value, cancellationToken).ConfigureAwait(false);
162127
}
163128

164129
async Task<bool> ICacheClientAsync.ReplaceAsync<T>(string key, T value, CancellationToken cancellationToken)
165130
{
166-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
167-
await using (client as IAsyncDisposable)
168-
{
169-
return await client.ReplaceAsync<T>(key, value, cancellationToken).ConfigureAwait(false);
170-
}
131+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
132+
return await client.ReplaceAsync<T>(key, value, cancellationToken).ConfigureAwait(false);
171133
}
172134

173135
async Task<bool> ICacheClientAsync.AddAsync<T>(string key, T value, DateTime expiresAt, CancellationToken cancellationToken)
174136
{
175-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
176-
await using (client as IAsyncDisposable)
177-
{
178-
return await client.AddAsync<T>(key, value, expiresAt, cancellationToken).ConfigureAwait(false);
179-
}
137+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
138+
return await client.AddAsync<T>(key, value, expiresAt, cancellationToken).ConfigureAwait(false);
180139
}
181140

182141
async Task<bool> ICacheClientAsync.ReplaceAsync<T>(string key, T value, DateTime expiresAt, CancellationToken cancellationToken)
183142
{
184-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
185-
await using (client as IAsyncDisposable)
186-
{
187-
return await client.ReplaceAsync<T>(key, value, expiresAt, cancellationToken).ConfigureAwait(false);
188-
}
143+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
144+
return await client.ReplaceAsync<T>(key, value, expiresAt, cancellationToken).ConfigureAwait(false);
189145
}
190146

191147
async Task<bool> ICacheClientAsync.AddAsync<T>(string key, T value, TimeSpan expiresIn, CancellationToken cancellationToken)
192148
{
193-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
194-
await using (client as IAsyncDisposable)
195-
{
196-
return await client.AddAsync<T>(key, value, expiresIn, cancellationToken).ConfigureAwait(false);
197-
}
149+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
150+
return await client.AddAsync<T>(key, value, expiresIn, cancellationToken).ConfigureAwait(false);
198151
}
199152

200153
async Task<bool> ICacheClientAsync.ReplaceAsync<T>(string key, T value, TimeSpan expiresIn, CancellationToken cancellationToken)
201154
{
202-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
203-
await using (client as IAsyncDisposable)
204-
{
205-
return await client.ReplaceAsync<T>(key, value, expiresIn, cancellationToken).ConfigureAwait(false);
206-
}
155+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
156+
return await client.ReplaceAsync<T>(key, value, expiresIn, cancellationToken).ConfigureAwait(false);
207157
}
208158

209159
async Task<TimeSpan?> ICacheClientAsync.GetTimeToLiveAsync(string key, CancellationToken cancellationToken)
210160
{
211-
var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
212-
await using (client as IAsyncDisposable)
213-
{
214-
return await client.GetTimeToLiveAsync(key, cancellationToken).ConfigureAwait(false);
215-
}
161+
await using var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
162+
return await client.GetTimeToLiveAsync(key, cancellationToken).ConfigureAwait(false);
216163
}
217164

218-
async Task<IEnumerable<string>> ICacheClientAsync.GetKeysByPatternAsync(string pattern, CancellationToken cancellationToken)
165+
async IAsyncEnumerable<string> ICacheClientAsync.GetKeysByPatternAsync(string pattern, [EnumeratorCancellation] CancellationToken cancellationToken)
219166
{
220-
var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
221-
await using (client as IAsyncDisposable)
167+
await using var client = await GetReadOnlyCacheClientAsync(cancellationToken).ConfigureAwait(false);
168+
await foreach (var key in client.GetKeysByPatternAsync(pattern, cancellationToken).ConfigureAwait(false).WithCancellation(cancellationToken))
222169
{
223-
return await client.GetKeysByPatternAsync(pattern, cancellationToken).ConfigureAwait(false);
170+
yield return key;
224171
}
225172
}
226173

227174
async Task ICacheClientAsync.RemoveExpiredEntriesAsync(CancellationToken cancellationToken)
228175
{
229-
var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
230-
await using (client as IAsyncDisposable)
231-
{
232-
await client.RemoveExpiredEntriesAsync(cancellationToken).ConfigureAwait(false);
233-
}
176+
await using var client = await GetCacheClientAsync(cancellationToken).ConfigureAwait(false);
177+
await client.RemoveExpiredEntriesAsync(cancellationToken).ConfigureAwait(false);
234178
}
235179
}
236180
}

src/ServiceStack.Redis/RedisClient.Async.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,13 @@ Task<IDictionary<string, T>> ICacheClientAsync.GetAllAsync<T>(IEnumerable<string
439439
Task<bool> ICacheClientAsync.RemoveAsync(string key, CancellationToken cancellationToken)
440440
=> NativeAsync.DelAsync(key, cancellationToken).IsSuccessTaskAsync();
441441

442-
async Task<IEnumerable<string>> ICacheClientAsync.GetKeysByPatternAsync(string pattern, CancellationToken cancellationToken)
443-
{
444-
// buffer to match shape
445-
var list = new List<string>();
446-
await foreach (var key in AsAsync().ScanAllKeysAsync(pattern, cancellationToken: cancellationToken).ConfigureAwait(false).WithCancellation(cancellationToken))
447-
{
448-
list.Add(key);
449-
}
450-
return list;
451-
}
442+
IAsyncEnumerable<string> ICacheClientAsync.GetKeysByPatternAsync(string pattern, CancellationToken cancellationToken)
443+
=> AsAsync().ScanAllKeysAsync(pattern, cancellationToken: cancellationToken);
452444

453445
Task ICacheClientAsync.RemoveExpiredEntriesAsync(CancellationToken cancellationToken)
454446
{
455447
//Redis automatically removed expired Cache Entries
456-
return default;
448+
return Task.CompletedTask;
457449
}
458450

459451
async Task IRemoveByPatternAsync.RemoveByPatternAsync(string pattern, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)