diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs index 3c2d696d..96b7050e 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs @@ -159,11 +159,12 @@ public override async Task BaseGetAsync(string cacheKey, Type type, Canc /// The async. /// Cache key. /// CancellationToken - public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) + public override Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await Task.Run(() => { _cache.Remove(cacheKey); }, cancellationToken); + _cache.Remove(cacheKey); + return Task.CompletedTask; } /// @@ -175,7 +176,7 @@ public override async Task BaseRemoveAsync(string cacheKey, CancellationToken ca /// Expiration. /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) + public override Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -187,12 +188,10 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp expiration = expiration.Add(TimeSpan.FromSeconds(addSec)); } - await Task.Run(() => - { - //var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2); - //var val = new CacheValue(cacheValue, true, valExpiration); - _cache.Set(cacheKey, cacheValue, expiration); - }, cancellationToken); + //var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2); + //var val = new CacheValue(cacheValue, true, valExpiration); + _cache.Set(cacheKey, cacheValue, expiration); + return Task.CompletedTask; } /// @@ -201,11 +200,11 @@ await Task.Run(() => /// The async. /// Cache key. /// CancellationToken - public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) + public override Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.FromResult(_cache.Exists(cacheKey)); + return Task.FromResult(_cache.Exists(cacheKey)); } /// @@ -214,14 +213,15 @@ public override async Task BaseExistsAsync(string cacheKey, CancellationTo /// The by prefix async. /// Prefix. /// CancellationToken - public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) + public override Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - var count = await Task.Run(() => _cache.RemoveByPrefix(prefix), cancellationToken); + var count = _cache.RemoveByPrefix(prefix); if (_options.EnableLogging) _logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix} , count = {count}"); + return Task.CompletedTask; } /// @@ -230,17 +230,18 @@ public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationTo /// The by prefix async. /// Pattern. /// CancellationToken - public override async Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default) + public override Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(pattern, nameof(pattern)); var searchPattern = this.ProcessSearchKeyPattern(pattern); var searchKey = this.HandleSearchKeyPattern(pattern); - - var count = await Task.Run(() => _cache.RemoveByPattern(searchKey, searchPattern), cancellationToken); + + var count = _cache.RemoveByPattern(searchKey, searchPattern); if (_options.EnableLogging) _logger?.LogInformation($"BaseRemoveByPatternAsync : pattern = {pattern} , count = {count}"); + return Task.CompletedTask; } /// @@ -251,12 +252,13 @@ public override async Task BaseRemoveByPatternAsync(string pattern, Cancellation /// Expiration. /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) + public override Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); ArgumentCheck.NotNullAndCountGTZero(values, nameof(values)); - await Task.Run(() => _cache.SetAll(values, expiration), cancellationToken); + _cache.SetAll(values, expiration); + return Task.CompletedTask; } /// @@ -266,16 +268,16 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim /// Cache keys. /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) + public override Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); if (_options.EnableLogging) _logger?.LogInformation($"GetAllAsync : cacheKeys = {string.Join(",", cacheKeys)}"); - return await Task.FromResult(_cache.GetAll(cacheKeys)); + return Task.FromResult(_cache.GetAll(cacheKeys)); } - + /// /// Get all cacheKey by prefix async. @@ -298,7 +300,7 @@ public override Task> BaseGetAllKeysByPrefixAsync(string pre /// Prefix. /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) + public override Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); var map = new Dictionary>(); @@ -306,7 +308,7 @@ public override async Task>> BaseGetByPrefixAs if (_options.EnableLogging) _logger?.LogInformation($"GetByPrefixAsync : prefix = {prefix}"); - return await Task.FromResult(_cache.GetByPrefix(prefix)); + return Task.FromResult(_cache.GetByPrefix(prefix)); } /// @@ -315,14 +317,15 @@ public override async Task>> BaseGetByPrefixAs /// The all async. /// Cache keys. /// CancellationToken - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) + public override Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); if (_options.EnableLogging) _logger?.LogInformation($"RemoveAllAsync : cacheKeys = {string.Join(",", cacheKeys)}"); - await Task.Run(() => _cache.RemoveAll(cacheKeys), cancellationToken); + _cache.RemoveAll(cacheKeys); + return Task.CompletedTask; } /// @@ -330,13 +333,13 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, Can /// /// The async. /// CancellationToken - public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) + public override Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("FlushAsync"); _cache.Clear(); - await Task.CompletedTask; + return Task.CompletedTask; } ///