Skip to content

Commit 0a45d87

Browse files
authored
Updated InMemoryCachingProvider to stop creating unnecessary threads (#480)
1 parent 3630012 commit 0a45d87

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ public override async Task<object> BaseGetAsync(string cacheKey, Type type, Canc
159159
/// <returns>The async.</returns>
160160
/// <param name="cacheKey">Cache key.</param>
161161
/// <param name="cancellationToken">CancellationToken</param>
162-
public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default)
162+
public override Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default)
163163
{
164164
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
165165

166-
await Task.Run(() => { _cache.Remove(cacheKey); }, cancellationToken);
166+
_cache.Remove(cacheKey);
167+
return Task.CompletedTask;
167168
}
168169

169170
/// <summary>
@@ -175,7 +176,7 @@ public override async Task BaseRemoveAsync(string cacheKey, CancellationToken ca
175176
/// <param name="expiration">Expiration.</param>
176177
/// <param name="cancellationToken">CancellationToken</param>
177178
/// <typeparam name="T">The 1st type parameter.</typeparam>
178-
public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default)
179+
public override Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default)
179180
{
180181
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
181182
ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls);
@@ -187,12 +188,10 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
187188
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
188189
}
189190

190-
await Task.Run(() =>
191-
{
192-
//var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2);
193-
//var val = new CacheValue<T>(cacheValue, true, valExpiration);
194-
_cache.Set(cacheKey, cacheValue, expiration);
195-
}, cancellationToken);
191+
//var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2);
192+
//var val = new CacheValue<T>(cacheValue, true, valExpiration);
193+
_cache.Set(cacheKey, cacheValue, expiration);
194+
return Task.CompletedTask;
196195
}
197196

198197
/// <summary>
@@ -201,11 +200,11 @@ await Task.Run(() =>
201200
/// <returns>The async.</returns>
202201
/// <param name="cacheKey">Cache key.</param>
203202
/// <param name="cancellationToken">CancellationToken</param>
204-
public override async Task<bool> BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default)
203+
public override Task<bool> BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default)
205204
{
206205
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
207206

208-
return await Task.FromResult(_cache.Exists(cacheKey));
207+
return Task.FromResult(_cache.Exists(cacheKey));
209208
}
210209

211210
/// <summary>
@@ -214,14 +213,15 @@ public override async Task<bool> BaseExistsAsync(string cacheKey, CancellationTo
214213
/// <returns>The by prefix async.</returns>
215214
/// <param name="prefix">Prefix.</param>
216215
/// <param name="cancellationToken">CancellationToken</param>
217-
public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
216+
public override Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default)
218217
{
219218
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));
220219

221-
var count = await Task.Run(() => _cache.RemoveByPrefix(prefix), cancellationToken);
220+
var count = _cache.RemoveByPrefix(prefix);
222221

223222
if (_options.EnableLogging)
224223
_logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix} , count = {count}");
224+
return Task.CompletedTask;
225225
}
226226

227227
/// <summary>
@@ -230,17 +230,18 @@ public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationTo
230230
/// <returns>The by prefix async.</returns>
231231
/// <param name="pattern">Pattern.</param>
232232
/// <param name="cancellationToken">CancellationToken</param>
233-
public override async Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default)
233+
public override Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default)
234234
{
235235
ArgumentCheck.NotNullOrWhiteSpace(pattern, nameof(pattern));
236236

237237
var searchPattern = this.ProcessSearchKeyPattern(pattern);
238238
var searchKey = this.HandleSearchKeyPattern(pattern);
239-
240-
var count = await Task.Run(() => _cache.RemoveByPattern(searchKey, searchPattern), cancellationToken);
239+
240+
var count = _cache.RemoveByPattern(searchKey, searchPattern);
241241

242242
if (_options.EnableLogging)
243243
_logger?.LogInformation($"BaseRemoveByPatternAsync : pattern = {pattern} , count = {count}");
244+
return Task.CompletedTask;
244245
}
245246

246247
/// <summary>
@@ -251,12 +252,13 @@ public override async Task BaseRemoveByPatternAsync(string pattern, Cancellation
251252
/// <param name="expiration">Expiration.</param>
252253
/// <param name="cancellationToken">CancellationToken</param>
253254
/// <typeparam name="T">The 1st type parameter.</typeparam>
254-
public override async Task BaseSetAllAsync<T>(IDictionary<string, T> values, TimeSpan expiration, CancellationToken cancellationToken = default)
255+
public override Task BaseSetAllAsync<T>(IDictionary<string, T> values, TimeSpan expiration, CancellationToken cancellationToken = default)
255256
{
256257
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
257258
ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));
258259

259-
await Task.Run(() => _cache.SetAll(values, expiration), cancellationToken);
260+
_cache.SetAll(values, expiration);
261+
return Task.CompletedTask;
260262
}
261263

262264
/// <summary>
@@ -266,16 +268,16 @@ public override async Task BaseSetAllAsync<T>(IDictionary<string, T> values, Tim
266268
/// <param name="cacheKeys">Cache keys.</param>
267269
/// <param name="cancellationToken">CancellationToken</param>
268270
/// <typeparam name="T">The 1st type parameter.</typeparam>
269-
public override async Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T>(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
271+
public override Task<IDictionary<string, CacheValue<T>>> BaseGetAllAsync<T>(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
270272
{
271273
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));
272274

273275
if (_options.EnableLogging)
274276
_logger?.LogInformation($"GetAllAsync : cacheKeys = {string.Join(",", cacheKeys)}");
275277

276-
return await Task.FromResult(_cache.GetAll<T>(cacheKeys));
278+
return Task.FromResult(_cache.GetAll<T>(cacheKeys));
277279
}
278-
280+
279281

280282
/// <summary>
281283
/// Get all cacheKey by prefix async.
@@ -298,15 +300,15 @@ public override Task<IEnumerable<string>> BaseGetAllKeysByPrefixAsync(string pre
298300
/// <param name="prefix">Prefix.</param>
299301
/// <param name="cancellationToken">CancellationToken</param>
300302
/// <typeparam name="T">The 1st type parameter.</typeparam>
301-
public override async Task<IDictionary<string, CacheValue<T>>> BaseGetByPrefixAsync<T>(string prefix, CancellationToken cancellationToken = default)
303+
public override Task<IDictionary<string, CacheValue<T>>> BaseGetByPrefixAsync<T>(string prefix, CancellationToken cancellationToken = default)
302304
{
303305
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));
304306
var map = new Dictionary<string, CacheValue<T>>();
305307

306308
if (_options.EnableLogging)
307309
_logger?.LogInformation($"GetByPrefixAsync : prefix = {prefix}");
308310

309-
return await Task.FromResult(_cache.GetByPrefix<T>(prefix));
311+
return Task.FromResult(_cache.GetByPrefix<T>(prefix));
310312
}
311313

312314
/// <summary>
@@ -315,28 +317,29 @@ public override async Task<IDictionary<string, CacheValue<T>>> BaseGetByPrefixAs
315317
/// <returns>The all async.</returns>
316318
/// <param name="cacheKeys">Cache keys.</param>
317319
/// <param name="cancellationToken">CancellationToken</param>
318-
public override async Task BaseRemoveAllAsync(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
320+
public override Task BaseRemoveAllAsync(IEnumerable<string> cacheKeys, CancellationToken cancellationToken = default)
319321
{
320322
ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));
321323

322324
if (_options.EnableLogging)
323325
_logger?.LogInformation($"RemoveAllAsync : cacheKeys = {string.Join(",", cacheKeys)}");
324326

325-
await Task.Run(() => _cache.RemoveAll(cacheKeys), cancellationToken);
327+
_cache.RemoveAll(cacheKeys);
328+
return Task.CompletedTask;
326329
}
327330

328331
/// <summary>
329332
/// Flush All Cached Item async.
330333
/// </summary>
331334
/// <returns>The async.</returns>
332335
/// <param name="cancellationToken">CancellationToken</param>
333-
public override async Task BaseFlushAsync(CancellationToken cancellationToken = default)
336+
public override Task BaseFlushAsync(CancellationToken cancellationToken = default)
334337
{
335338
if (_options.EnableLogging)
336339
_logger?.LogInformation("FlushAsync");
337340

338341
_cache.Clear();
339-
await Task.CompletedTask;
342+
return Task.CompletedTask;
340343
}
341344

342345
/// <summary>

0 commit comments

Comments
 (0)