From 0edac98f5fd84da03e057e168eacb090fe9c96f5 Mon Sep 17 00:00:00 2001 From: st0o0 Date: Tue, 2 Aug 2022 08:17:25 +0200 Subject: [PATCH 1/5] init CancellationToken implementation --- .../DefaultCSRedisCachingProvider.Async.cs | 54 +++++---- .../EasyCachingAbstractProvider.cs | 95 ++++++++-------- src/EasyCaching.Core/IEasyCachingProvider.cs | 16 ++- .../IEasyCachingProviderBase.cs | 31 +++-- .../DefaultDiskCachingProvider.Async.cs | 71 ++++++------ .../HybridCachingProvider.cs | 95 +++++++++------- .../DefaultInMemoryCachingProvider.Async.cs | 67 ++++++----- .../DefaultLiteDBCachingProvider.Async.cs | 74 +++++++----- .../DefaultSQLiteCachingProvider.Async.cs | 107 +++++++++++------- 9 files changed, 355 insertions(+), 255 deletions(-) diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs index e85c3831..501d0778 100644 --- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs +++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using global::CSRedis; @@ -14,32 +15,35 @@ public partial class DefaultCSRedisCachingProvider : EasyCachingAbstractProvider /// /// The async. /// Cache key. - public override async Task BaseExistsAsync(string cacheKey) + /// CancellationToken + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); return await _cache.ExistsAsync(cacheKey); } - + /// /// Flushs the async. /// + /// CancellationToken /// The async. - public override async Task BaseFlushAsync() + public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("Redis -- FlushAsync"); await _cache.NodesServerManager.FlushDbAsync(); } - + /// /// Gets all async. /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -67,8 +71,9 @@ public override async Task>> BaseGetAllAsyncCache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -120,7 +125,8 @@ public override async Task> BaseGetAsync(string cacheKey, Func< /// The async. /// Cache key. /// Object Type. - public override async Task BaseGetAsync(string cacheKey, Type type) + /// CancellationToken + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -151,8 +157,9 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -183,7 +190,8 @@ public override async Task> BaseGetAsync(string cacheKey) /// /// The count. /// Prefix. - public override Task BaseGetCountAsync(string prefix = "") + /// CancellationToken + public override Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(prefix)) { @@ -207,8 +215,9 @@ public override Task BaseGetCountAsync(string prefix = "") /// /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix) + public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -229,13 +238,14 @@ public override async Task>> BaseGetByPrefixAs return result; } - + /// /// Removes all async. /// /// The all async. /// Cache keys. - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -254,19 +264,21 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) /// /// The async. /// Cache key. - public override async Task BaseRemoveAsync(string cacheKey) + /// CancellationToken + public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); await _cache.DelAsync(cacheKey); - } + } /// /// Removes the by prefix async. /// /// The by prefix async. /// Prefix. - public override async Task BaseRemoveByPrefixAsync(string prefix) + /// CancellationToken + public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -293,8 +305,9 @@ public override async Task BaseRemoveByPrefixAsync(string prefix) /// The all async. /// Value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary value, TimeSpan expiration) + public override async Task BaseSetAllAsync(IDictionary value, TimeSpan expiration, CancellationToken cancellationToken = default) { //whether to use pipe based on redis mode var tasks = new List>(); @@ -320,8 +333,9 @@ public override async Task BaseSetAllAsync(IDictionary value, Time /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -349,8 +363,9 @@ await _cache.SetAsync( /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -374,8 +389,9 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu /// Get the expiration of cache key async /// /// cache key + /// CancellationToken /// expiration - public override async Task BaseGetExpirationAsync(string cacheKey) + public override async Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); diff --git a/src/EasyCaching.Core/EasyCachingAbstractProvider.cs b/src/EasyCaching.Core/EasyCachingAbstractProvider.cs index 8dac2e5a..6f06028d 100644 --- a/src/EasyCaching.Core/EasyCachingAbstractProvider.cs +++ b/src/EasyCaching.Core/EasyCachingAbstractProvider.cs @@ -6,6 +6,7 @@ namespace EasyCaching.Core using System.Collections.Generic; using System.Diagnostics; using System.Linq; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core.Configurations; using EasyCaching.Core.Diagnostics; @@ -43,35 +44,35 @@ protected EasyCachingAbstractProvider(IDistributedLockFactory lockFactory, BaseP public abstract object BaseGetDatabse(); public abstract bool BaseExists(string cacheKey); - public abstract Task BaseExistsAsync(string cacheKey); + public abstract Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default); public abstract void BaseFlush(); - public abstract Task BaseFlushAsync(); + public abstract Task BaseFlushAsync(CancellationToken cancellationToken = default); public abstract CacheValue BaseGet(string cacheKey, Func dataRetriever, TimeSpan expiration); public abstract CacheValue BaseGet(string cacheKey); public abstract IDictionary> BaseGetAll(IEnumerable cacheKeys); - public abstract Task>> BaseGetAllAsync(IEnumerable cacheKeys); - public abstract Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration); - public abstract Task BaseGetAsync(string cacheKey, Type type); - public abstract Task> BaseGetAsync(string cacheKey); + public abstract Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default); + public abstract Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default); + public abstract Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default); + public abstract Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default); public abstract IDictionary> BaseGetByPrefix(string prefix); - public abstract Task>> BaseGetByPrefixAsync(string prefix); + public abstract Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default); public abstract int BaseGetCount(string prefix = ""); - public abstract Task BaseGetCountAsync(string prefix = ""); + public abstract Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default); public abstract void BaseRemove(string cacheKey); public abstract void BaseRemoveAll(IEnumerable cacheKeys); - public abstract Task BaseRemoveAllAsync(IEnumerable cacheKeys); - public abstract Task BaseRemoveAsync(string cacheKey); + public abstract Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellation = default); + public abstract Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default); public abstract void BaseRemoveByPrefix(string prefix); - public abstract Task BaseRemoveByPrefixAsync(string prefix); + public abstract Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default); public abstract void BaseSet(string cacheKey, T cacheValue, TimeSpan expiration); public abstract void BaseSetAll(IDictionary values, TimeSpan expiration); - public abstract Task BaseSetAllAsync(IDictionary values, TimeSpan expiration); - public abstract Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration); + public abstract Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default); + public abstract Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default); public abstract bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expiration); - public abstract Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration); + public abstract Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default); public abstract TimeSpan BaseGetExpiration(string cacheKey); - public abstract Task BaseGetExpirationAsync(string cacheKey); + public abstract Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default); public abstract ProviderInfo BaseGetProviderInfo(); public bool Exists(string cacheKey) @@ -100,13 +101,13 @@ public bool Exists(string cacheKey) } } - public async Task ExistsAsync(string cacheKey) + public async Task ExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteExistsCacheBefore(new BeforeExistsRequestEventData(CachingProviderType.ToString(), Name, nameof(ExistsAsync), cacheKey)); Exception e = null; try { - var flag = await BaseExistsAsync(cacheKey); + var flag = await BaseExistsAsync(cacheKey, cancellationToken); return flag; } catch (Exception ex) @@ -153,13 +154,13 @@ public void Flush() } } - public async Task FlushAsync() + public async Task FlushAsync(CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteFlushCacheBefore(new EventData(CachingProviderType.ToString(), Name, nameof(FlushAsync))); Exception e = null; try { - await BaseFlushAsync(); + await BaseFlushAsync(cancellationToken); } catch (Exception ex) { @@ -280,13 +281,13 @@ public IDictionary> GetAll(IEnumerable cacheKey } } - public async Task>> GetAllAsync(IEnumerable cacheKeys) + public async Task>> GetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAllAsync), cacheKeys.ToArray())); Exception e = null; try { - return await BaseGetAllAsync(cacheKeys); + return await BaseGetAllAsync(cacheKeys, cancellationToken); } catch (Exception ex) { @@ -306,13 +307,13 @@ public async Task>> GetAllAsync(IEnumerable } } - public async Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public async Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAsync), new[] { cacheKey }, expiration)); Exception e = null; try { - if (_lockFactory == null) return await BaseGetAsync(cacheKey, dataRetriever, expiration); + if (_lockFactory == null) return await BaseGetAsync(cacheKey, dataRetriever, expiration, cancellationToken); var value = await BaseGetAsync(cacheKey); if (value.HasValue) return value; @@ -322,7 +323,7 @@ public async Task> GetAsync(string cacheKey, Func> data { if (!await @lock.LockAsync(_options.SleepMs)) throw new TimeoutException(); - value = await BaseGetAsync(cacheKey); + value = await BaseGetAsync(cacheKey, cancellationToken); if (value.HasValue) return value; var task = dataRetriever(); @@ -333,7 +334,7 @@ await Task.WhenAny(task, Task.Delay(_options.LockMs)) != task) var item = await task; if (item != null || _options.CacheNulls) { - await BaseSetAsync(cacheKey, item, expiration); + await BaseSetAsync(cacheKey, item, expiration, cancellationToken); return new CacheValue(item, true); } @@ -365,13 +366,13 @@ await Task.WhenAny(task, Task.Delay(_options.LockMs)) != task) } } - public async Task GetAsync(string cacheKey, Type type) + public async Task GetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, "GetAsync_Type", new[] { cacheKey })); Exception e = null; try { - return await BaseGetAsync(cacheKey, type); + return await BaseGetAsync(cacheKey, type, cancellationToken); } catch (Exception ex) { @@ -391,13 +392,13 @@ public async Task GetAsync(string cacheKey, Type type) } } - public async Task> GetAsync(string cacheKey) + public async Task> GetAsync(string cacheKey, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAsync), new[] { cacheKey })); Exception e = null; try { - return await BaseGetAsync(cacheKey); + return await BaseGetAsync(cacheKey, cancellationToken); } catch (Exception ex) { @@ -443,13 +444,13 @@ public IDictionary> GetByPrefix(string prefix) } } - public async Task>> GetByPrefixAsync(string prefix) + public async Task>> GetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetByPrefixAsync), new[] { prefix })); Exception e = null; try { - return await BaseGetByPrefixAsync(prefix); + return await BaseGetByPrefixAsync(prefix, cancellationToken); } catch (Exception ex) { @@ -474,9 +475,9 @@ public int GetCount(string prefix = "") return BaseGetCount(prefix); } - public async Task GetCountAsync(string prefix = "") + public async Task GetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { - return await BaseGetCountAsync(prefix); + return await BaseGetCountAsync(prefix, cancellationToken); } public void Remove(string cacheKey) @@ -531,13 +532,13 @@ public void RemoveAll(IEnumerable cacheKeys) } } - public async Task RemoveAllAsync(IEnumerable cacheKeys) + public async Task RemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteRemoveCacheBefore(new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveAllAsync), cacheKeys.ToArray())); Exception e = null; try { - await BaseRemoveAllAsync(cacheKeys); + await BaseRemoveAllAsync(cacheKeys, cancellationToken); } catch (Exception ex) { @@ -557,13 +558,13 @@ public async Task RemoveAllAsync(IEnumerable cacheKeys) } } - public async Task RemoveAsync(string cacheKey) + public async Task RemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteRemoveCacheBefore(new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveAsync), new[] { cacheKey })); Exception e = null; try { - await BaseRemoveAsync(cacheKey); + await BaseRemoveAsync(cacheKey, cancellationToken); } catch (Exception ex) { @@ -609,13 +610,13 @@ public void RemoveByPrefix(string prefix) } } - public async Task RemoveByPrefixAsync(string prefix) + public async Task RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteRemoveCacheBefore(new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveByPrefixAsync), new[] { prefix })); Exception e = null; try { - await BaseRemoveByPrefixAsync(prefix); + await BaseRemoveByPrefixAsync(prefix, cancellationToken); } catch (Exception ex) { @@ -687,13 +688,13 @@ public void SetAll(IDictionary value, TimeSpan expiration) } } - public async Task SetAllAsync(IDictionary value, TimeSpan expiration) + public async Task SetAllAsync(IDictionary value, TimeSpan expiration, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteSetCacheBefore(new BeforeSetRequestEventData(CachingProviderType.ToString(), Name, nameof(SetAllAsync), value.ToDictionary(k => k.Key, v => (object)v.Value), expiration)); Exception e = null; try { - await BaseSetAllAsync(value, expiration); + await BaseSetAllAsync(value, expiration, cancellationToken); } catch (Exception ex) { @@ -713,13 +714,13 @@ public async Task SetAllAsync(IDictionary value, TimeSpan expirati } } - public async Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public async Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteSetCacheBefore(new BeforeSetRequestEventData(CachingProviderType.ToString(), Name, nameof(SetAsync), new Dictionary { { cacheKey, cacheValue } }, expiration)); Exception e = null; try { - await BaseSetAsync(cacheKey, cacheValue, expiration); + await BaseSetAsync(cacheKey, cacheValue, expiration, cancellationToken); } catch (Exception ex) { @@ -765,13 +766,13 @@ public bool TrySet(string cacheKey, T cacheValue, TimeSpan expiration) } } - public async Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public async Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteSetCacheBefore(new BeforeSetRequestEventData(CachingProviderType.ToString(), Name, nameof(TrySetAsync), new Dictionary { { cacheKey, cacheValue } }, expiration)); Exception e = null; try { - return await BaseTrySetAsync(cacheKey, cacheValue, expiration); + return await BaseTrySetAsync(cacheKey, cacheValue, expiration, cancellationToken); } catch (Exception ex) { @@ -796,9 +797,9 @@ public TimeSpan GetExpiration(string cacheKey) return BaseGetExpiration(cacheKey); } - public async Task GetExpirationAsync(string cacheKey) + public async Task GetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { - return await BaseGetExpirationAsync(cacheKey); + return await BaseGetExpirationAsync(cacheKey, cancellationToken); } public ProviderInfo GetProviderInfo() diff --git a/src/EasyCaching.Core/IEasyCachingProvider.cs b/src/EasyCaching.Core/IEasyCachingProvider.cs index 0aca8d21..4606904b 100644 --- a/src/EasyCaching.Core/IEasyCachingProvider.cs +++ b/src/EasyCaching.Core/IEasyCachingProvider.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; /// @@ -34,8 +35,9 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - Task>> GetAllAsync(IEnumerable cacheKeys); + Task>> GetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default); /// /// Gets the by prefix. @@ -50,8 +52,9 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase /// /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - Task>> GetByPrefixAsync(string prefix); + Task>> GetByPrefixAsync(string prefix, CancellationToken cancellationToken = default); /// /// Gets the count. @@ -65,7 +68,8 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase /// /// The count. /// Prefix. - Task GetCountAsync(string prefix = ""); + /// CancellationToken + Task GetCountAsync(string prefix = "", CancellationToken cancellationToken = default); /// /// Flush All Cached Item. @@ -76,7 +80,8 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase /// Flush All Cached Item async. /// /// The async. - Task FlushAsync(); + /// CancellationToken + Task FlushAsync(CancellationToken cancellationToken = default); /// /// Gets the max rd second. @@ -107,8 +112,9 @@ public interface IEasyCachingProvider : IEasyCachingProviderBase /// Gets the exporation of specify cachekey async. /// /// + /// CancellationToken /// - Task GetExpirationAsync(string cacheKey); + Task GetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default); /// /// Gets the information of provider. diff --git a/src/EasyCaching.Core/IEasyCachingProviderBase.cs b/src/EasyCaching.Core/IEasyCachingProviderBase.cs index a7fce998..9b232e18 100644 --- a/src/EasyCaching.Core/IEasyCachingProviderBase.cs +++ b/src/EasyCaching.Core/IEasyCachingProviderBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace EasyCaching.Core @@ -28,8 +29,9 @@ public interface IEasyCachingProviderBase /// Cache key. /// Cache value. /// Expiration. + /// /// The 1st type parameter. - Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration); + Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default); /// /// Get the specified cacheKey. @@ -44,8 +46,9 @@ public interface IEasyCachingProviderBase /// /// The async. /// Cache key. + /// /// The 1st type parameter. - Task> GetAsync(string cacheKey); + Task> GetAsync(string cacheKey, CancellationToken cancellationToken = default); /// /// Remove the specified cacheKey. @@ -58,14 +61,16 @@ public interface IEasyCachingProviderBase /// /// The async. /// Cache key. - Task RemoveAsync(string cacheKey); + /// + Task RemoveAsync(string cacheKey, CancellationToken cancellationToken = default); /// /// Exists the specified cacheKey async. /// /// The async. /// Cache key. - Task ExistsAsync(string cacheKey); + /// + Task ExistsAsync(string cacheKey, CancellationToken cancellationToken = default); /// /// Exists the specified cacheKey. @@ -91,8 +96,9 @@ public interface IEasyCachingProviderBase /// Cache key. /// Cache value. /// Expiration. + /// /// The 1st type parameter. - Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration); + Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default); /// /// Sets all. @@ -108,8 +114,9 @@ public interface IEasyCachingProviderBase /// The all async. /// Value. /// Expiration. + /// /// The 1st type parameter. - Task SetAllAsync(IDictionary value, TimeSpan expiration); + Task SetAllAsync(IDictionary value, TimeSpan expiration, CancellationToken cancellationToken = default); /// /// Removes all. @@ -122,7 +129,8 @@ public interface IEasyCachingProviderBase /// /// The all async. /// Cache keys. - Task RemoveAllAsync(IEnumerable cacheKeys); + /// + Task RemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default); /// /// Get the specified cacheKey, dataRetriever and expiration. @@ -141,8 +149,9 @@ public interface IEasyCachingProviderBase /// Cache key. /// Data retriever. /// Expiration. + /// /// The 1st type parameter. - Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration); + Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default); /// /// Removes cached item by cachekey's prefix. @@ -154,7 +163,8 @@ public interface IEasyCachingProviderBase /// Removes cached item by cachekey's prefix async. /// /// Prefix of CacheKey. - Task RemoveByPrefixAsync(string prefix); + /// + Task RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default); /// /// Gets the specified cacheKey async. @@ -162,6 +172,7 @@ public interface IEasyCachingProviderBase /// The async. /// Cache key. /// Object Type. - Task GetAsync(string cacheKey, Type type); + /// + Task GetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/EasyCaching.Disk/DefaultDiskCachingProvider.Async.cs b/src/EasyCaching.Disk/DefaultDiskCachingProvider.Async.cs index b96ace0b..4e74545e 100644 --- a/src/EasyCaching.Disk/DefaultDiskCachingProvider.Async.cs +++ b/src/EasyCaching.Disk/DefaultDiskCachingProvider.Async.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using MessagePack; @@ -12,7 +13,7 @@ public partial class DefaultDiskCachingProvider : EasyCachingAbstractProvider { - public override async Task BaseExistsAsync(string cacheKey) + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -20,12 +21,12 @@ public override async Task BaseExistsAsync(string cacheKey) if (!File.Exists(path)) return false; - var val = await GetDiskCacheValueAsync(path); + var val = await GetDiskCacheValueAsync(path, cancellationToken); return val.Expiration > DateTimeOffset.UtcNow; } - public override Task BaseFlushAsync() + public override Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("FlushAsync"); @@ -41,7 +42,7 @@ public override Task BaseFlushAsync() return Task.CompletedTask; } - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -60,11 +61,11 @@ public override async Task>> BaseGetAllAsync DateTimeOffset.UtcNow) { - var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance)); + var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance), cancellationToken); if (!dict.ContainsKey(item)) { @@ -84,7 +85,7 @@ public override async Task>> BaseGetAllAsync> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -99,13 +100,13 @@ public override async Task> BaseGetAsync(string cacheKey, Func< MessagePack.MessagePackSerializationException : Failed to deserialize EasyCaching.Disk.DiskCacheValue value. ---- System.IO.EndOfStreamException : Attempted to read past the end of the stream. */ - var cached = await GetDiskCacheValueAsync(path); + var cached = await GetDiskCacheValueAsync(path, cancellationToken); //var cached = GetDiskCacheValueAsync(path).ConfigureAwait(false).GetAwaiter().GetResult(); //var cached = GetDiskCacheValue(path); if (cached.Expiration > DateTimeOffset.UtcNow) { - var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance)); + var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance), cancellationToken); if (_options.EnableLogging) _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); @@ -125,14 +126,14 @@ public override async Task> BaseGetAsync(string cacheKey, Func< if (!_cacheKeysMap.TryAdd($"{cacheKey}_Lock", "1")) { System.Threading.Thread.Sleep(_options.SleepMs); - return await GetAsync(cacheKey, dataRetriever, expiration); + return await GetAsync(cacheKey, dataRetriever, expiration, cancellationToken); } var res = await dataRetriever(); if (res != null || _options.CacheNulls) { - await SetAsync(cacheKey, res, expiration); + await SetAsync(cacheKey, res, expiration, cancellationToken); //remove mutex key _cacheKeysMap.TryRemove($"{cacheKey}_Lock", out _); return new CacheValue(res, true); @@ -145,7 +146,7 @@ public override async Task> BaseGetAsync(string cacheKey, Func< } } - public override Task BaseGetCountAsync(string prefix = "") + public override Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(prefix)) { @@ -157,7 +158,7 @@ public override Task BaseGetCountAsync(string prefix = "") } } - public override async Task BaseGetAsync(string cacheKey, Type type) + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -173,7 +174,7 @@ public override async Task BaseGetAsync(string cacheKey, Type type) return null; } - var cached = await GetDiskCacheValueAsync(path); + var cached = await GetDiskCacheValueAsync(path, cancellationToken); if (cached.Expiration > DateTimeOffset.UtcNow) { @@ -182,7 +183,7 @@ public override async Task BaseGetAsync(string cacheKey, Type type) CacheStats.OnHit(); - var t = MessagePackSerializer.Deserialize(type, cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance)); + var t = MessagePackSerializer.Deserialize(type, cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance), cancellationToken); return t; } else @@ -196,7 +197,7 @@ public override async Task BaseGetAsync(string cacheKey, Type type) } } - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -212,7 +213,7 @@ public override async Task> BaseGetAsync(string cacheKey) return CacheValue.NoValue; } - var cached = await GetDiskCacheValueAsync(path); + var cached = await GetDiskCacheValueAsync(path, cancellationToken); if (cached.Expiration > DateTimeOffset.UtcNow) { @@ -221,7 +222,7 @@ public override async Task> BaseGetAsync(string cacheKey) CacheStats.OnHit(); - var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance)); + var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance), cancellationToken); return new CacheValue(t, true); } else @@ -235,7 +236,7 @@ public override async Task> BaseGetAsync(string cacheKey) } } - public override async Task>> BaseGetByPrefixAsync(string prefix) + public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -258,11 +259,11 @@ public override async Task>> BaseGetByPrefixAs } else { - var cached = await GetDiskCacheValueAsync(path); + var cached = await GetDiskCacheValueAsync(path, cancellationToken); if (cached.Expiration > DateTimeOffset.UtcNow) { - var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance)); + var t = MessagePackSerializer.Deserialize(cached.Value, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance), cancellationToken); if (!dict.ContainsKey(item)) { @@ -282,7 +283,7 @@ public override async Task>> BaseGetByPrefixAs return dict; } - public override async Task BaseGetExpirationAsync(string cacheKey) + public override async Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -293,12 +294,12 @@ public override async Task BaseGetExpirationAsync(string cacheKey) return TimeSpan.Zero; } - var cached = await GetDiskCacheValueAsync(path); + var cached = await GetDiskCacheValueAsync(path, cancellationToken); return cached.Expiration.Subtract(DateTimeOffset.UtcNow); } - public override Task BaseRemoveAllAsync(IEnumerable cacheKeys) + public override Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -323,7 +324,7 @@ public override Task BaseRemoveAllAsync(IEnumerable cacheKeys) return Task.CompletedTask; } - public override Task BaseRemoveAsync(string cacheKey) + public override Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -343,7 +344,7 @@ public override Task BaseRemoveAsync(string cacheKey) return Task.CompletedTask; } - public override Task BaseRemoveByPrefixAsync(string prefix) + public override Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -362,7 +363,7 @@ public override Task BaseRemoveByPrefixAsync(string prefix) return Task.CompletedTask; } - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); ArgumentCheck.NotNullAndCountGTZero(values, nameof(values)); @@ -377,7 +378,7 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { - await stream.WriteAsync(bytes, 0, bytes.Length); + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); } AppendKey(item.Key, fileName); @@ -389,7 +390,7 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim } } - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -401,14 +402,14 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { - await stream.WriteAsync(bytes, 0, bytes.Length); + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); //return true; } AppendKey(cacheKey, fileName); } - public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -418,7 +419,7 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu if (File.Exists(path)) { - var cached = await GetDiskCacheValueAsync(path); + var cached = await GetDiskCacheValueAsync(path, cancellationToken); if (cached.Expiration.Subtract(DateTimeOffset.UtcNow) > TimeSpan.Zero) { @@ -430,17 +431,17 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) { - await stream.WriteAsync(bytes, 0, bytes.Length); + await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken); AppendKey(cacheKey, fileName); return true; } } - private async Task GetDiskCacheValueAsync(string path) + private async Task GetDiskCacheValueAsync(string path, CancellationToken cancellationToken = default) { using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { - var cached = await MessagePackSerializer.DeserializeAsync(stream, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance)); + var cached = await MessagePackSerializer.DeserializeAsync(stream, MessagePackSerializerOptions.Standard.WithResolver(ContractlessStandardResolver.Instance), cancellationToken); return cached; } diff --git a/src/EasyCaching.HybridCache/HybridCachingProvider.cs b/src/EasyCaching.HybridCache/HybridCachingProvider.cs index 44721990..55a41839 100644 --- a/src/EasyCaching.HybridCache/HybridCachingProvider.cs +++ b/src/EasyCaching.HybridCache/HybridCachingProvider.cs @@ -2,7 +2,8 @@ { using System; using System.Collections.Generic; - using System.Linq; + using System.Linq; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using EasyCaching.Core.Bus; @@ -177,7 +178,8 @@ public bool Exists(string cacheKey) /// /// The async. /// Cache key. - public async Task ExistsAsync(string cacheKey) + /// CancellationToken + public async Task ExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -203,6 +205,7 @@ public async Task ExistsAsync(string cacheKey) /// /// The get. /// Cache key. + /// CancellationToken /// The 1st type parameter. public CacheValue Get(string cacheKey) { @@ -246,12 +249,13 @@ public CacheValue Get(string cacheKey) /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public async Task> GetAsync(string cacheKey) + public async Task> GetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var cacheValue = await _localCache.GetAsync(cacheKey); + var cacheValue = await _localCache.GetAsync(cacheKey, cancellationToken); if (cacheValue.HasValue) { @@ -262,7 +266,7 @@ public async Task> GetAsync(string cacheKey) try { - cacheValue = await _distributedCache.GetAsync(cacheKey); + cacheValue = await _distributedCache.GetAsync(cacheKey, cancellationToken); } catch (Exception ex) { @@ -271,9 +275,9 @@ public async Task> GetAsync(string cacheKey) if (cacheValue.HasValue) { - TimeSpan ts = await GetExpirationAsync(cacheKey); + TimeSpan ts = await GetExpirationAsync(cacheKey, cancellationToken); - await _localCache.SetAsync(cacheKey, cacheValue.Value, ts); + await _localCache.SetAsync(cacheKey, cacheValue.Value, ts, cancellationToken); return cacheValue; } @@ -312,24 +316,25 @@ public void Remove(string cacheKey) /// /// The async. /// Cache key. - public async Task RemoveAsync(string cacheKey) + /// CancellationToken + public async Task RemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); try { // distributed cache at first - await _distributedCache.RemoveAsync(cacheKey); + await _distributedCache.RemoveAsync(cacheKey, cancellationToken); } catch (Exception ex) { LogMessage($"remove cache key [{cacheKey}] error", ex); } - await _localCache.RemoveAsync(cacheKey); + await _localCache.RemoveAsync(cacheKey, cancellationToken); // send message to bus - await _busAsyncWrap.ExecuteAsync(async () => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } })); + await _busAsyncWrap.ExecuteAsync(async (ct) => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } }, ct), cancellationToken); } /// @@ -365,16 +370,17 @@ public void Set(string cacheKey, T cacheValue, TimeSpan expiration) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public async Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public async Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await _localCache.SetAsync(cacheKey, cacheValue, expiration); + await _localCache.SetAsync(cacheKey, cacheValue, expiration, cancellationToken); try { - await _distributedCache.SetAsync(cacheKey, cacheValue, expiration); + await _distributedCache.SetAsync(cacheKey, cacheValue, expiration, cancellationToken); } catch (Exception ex) { @@ -382,7 +388,7 @@ public async Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration } // When create/update cache, send message to bus so that other clients can remove it. - await _busAsyncWrap.ExecuteAsync(async () => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } })); + await _busAsyncWrap.ExecuteAsync(async (ct) => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } }, ct), cancellationToken); } /// @@ -439,8 +445,9 @@ public bool TrySet(string cacheKey, T cacheValue, TimeSpan expiration) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public async Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public async Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -449,7 +456,7 @@ public async Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan e try { - flag = await _distributedCache.TrySetAsync(cacheKey, cacheValue, expiration); + flag = await _distributedCache.TrySetAsync(cacheKey, cacheValue, expiration, cancellationToken); } catch (Exception ex) { @@ -461,19 +468,19 @@ public async Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan e { // When we TrySet succeed in distributed cache, we should Set this cache to local cache. // It's mainly to prevent the cache value was changed - await _localCache.SetAsync(cacheKey, cacheValue, expiration); + await _localCache.SetAsync(cacheKey, cacheValue, expiration, cancellationToken); } // distributed cache occur error, have a try with local cache if (distributedError) { - flag = await _localCache.TrySetAsync(cacheKey, cacheValue, expiration); + flag = await _localCache.TrySetAsync(cacheKey, cacheValue, expiration, cancellationToken); } if (flag) { // Here should send message to bus due to cache was set successfully. - await _busAsyncWrap.ExecuteAsync(async () => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } })); + await _busAsyncWrap.ExecuteAsync(async (ct) => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { cacheKey } }, ct), cancellationToken); } return flag; @@ -508,14 +515,15 @@ public void SetAll(IDictionary value, TimeSpan expiration) /// The all async. /// Value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public async Task SetAllAsync(IDictionary value, TimeSpan expiration) + public async Task SetAllAsync(IDictionary value, TimeSpan expiration, CancellationToken cancellationToken = default) { - await _localCache.SetAllAsync(value, expiration); + await _localCache.SetAllAsync(value, expiration, cancellationToken); try { - await _distributedCache.SetAllAsync(value, expiration); + await _distributedCache.SetAllAsync(value, expiration, cancellationToken); } catch (Exception ex) { @@ -523,7 +531,7 @@ public async Task SetAllAsync(IDictionary value, TimeSpan expirati } // send message to bus - await _busAsyncWrap.ExecuteAsync(async () => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = value.Keys.ToArray(), IsPrefix = false })); + await _busAsyncWrap.ExecuteAsync(async (ct) => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = value.Keys.ToArray(), IsPrefix = false }, ct), cancellationToken); } /// @@ -554,23 +562,24 @@ public void RemoveAll(IEnumerable cacheKeys) /// /// The all async. /// Cache keys. - public async Task RemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public async Task RemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); try { - await _distributedCache.RemoveAllAsync(cacheKeys); + await _distributedCache.RemoveAllAsync(cacheKeys, cancellationToken); } catch (Exception ex) { LogMessage($"remove all async from distributed provider error [{string.Join(",", cacheKeys)}]", ex); } - await _localCache.RemoveAllAsync(cacheKeys); + await _localCache.RemoveAllAsync(cacheKeys, cancellationToken); // send message to bus in order to notify other clients. - await _busAsyncWrap.ExecuteAsync(async () => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = cacheKeys.ToArray() })); + await _busAsyncWrap.ExecuteAsync(async (ct) => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = cacheKeys.ToArray() }, ct), cancellationToken); } /// @@ -621,13 +630,14 @@ public CacheValue Get(string cacheKey, Func dataRetriever, TimeSpan exp /// Cache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public async Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public async Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - var result = await _localCache.GetAsync(cacheKey); + var result = await _localCache.GetAsync(cacheKey, cancellationToken); if (result.HasValue) { @@ -636,7 +646,7 @@ public async Task> GetAsync(string cacheKey, Func> data try { - result = await _distributedCache.GetAsync(cacheKey, dataRetriever, expiration); + result = await _distributedCache.GetAsync(cacheKey, dataRetriever, expiration, cancellationToken); } catch (Exception ex) { @@ -645,7 +655,7 @@ public async Task> GetAsync(string cacheKey, Func> data if (result.HasValue) { - TimeSpan ts = await GetExpirationAsync(cacheKey); + TimeSpan ts = await GetExpirationAsync(cacheKey, cancellationToken); _localCache.Set(cacheKey, result.Value, ts); @@ -683,13 +693,14 @@ public void RemoveByPrefix(string prefix) /// /// The by prefix async. /// Prefix. - public async Task RemoveByPrefixAsync(string prefix) + /// CancellationToken + public async Task RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); try { - await _distributedCache.RemoveByPrefixAsync(prefix); + await _distributedCache.RemoveByPrefixAsync(prefix, cancellationToken); } catch (Exception ex) { @@ -699,7 +710,7 @@ public async Task RemoveByPrefixAsync(string prefix) await _localCache.RemoveByPrefixAsync(prefix); // send message to bus in order to notify other clients. - await _busAsyncWrap.ExecuteAsync(async () => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { prefix }, IsPrefix = true })); + await _busAsyncWrap.ExecuteAsync(async (ct) => await _bus.PublishAsync(_options.TopicName, new EasyCachingMessage { Id = _cacheId, CacheKeys = new string[] { prefix }, IsPrefix = true }, ct), cancellationToken); } /// @@ -722,13 +733,13 @@ private void LogMessage(string message, Exception ex = null) } } - private async Task GetExpirationAsync(string cacheKey) + private async Task GetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { TimeSpan ts = TimeSpan.Zero; try { - ts = await _distributedCache.GetExpirationAsync(cacheKey); + ts = await _distributedCache.GetExpirationAsync(cacheKey, cancellationToken); } catch { @@ -764,11 +775,11 @@ private TimeSpan GetExpiration(string cacheKey) return ts; } - public async Task GetAsync(string cacheKey, Type type) + public async Task GetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var cacheValue = await _localCache.GetAsync(cacheKey, type); + var cacheValue = await _localCache.GetAsync(cacheKey, type, cancellationToken); if (cacheValue != null) { @@ -779,7 +790,7 @@ public async Task GetAsync(string cacheKey, Type type) try { - cacheValue = await _distributedCache.GetAsync(cacheKey, type); + cacheValue = await _distributedCache.GetAsync(cacheKey, type, cancellationToken); } catch (Exception ex) { @@ -788,9 +799,9 @@ public async Task GetAsync(string cacheKey, Type type) if (cacheValue != null) { - TimeSpan ts = await GetExpirationAsync(cacheKey); + TimeSpan ts = await GetExpirationAsync(cacheKey, cancellationToken); - await _localCache.SetAsync(cacheKey, cacheValue, ts); + await _localCache.SetAsync(cacheKey, cacheValue, ts, cancellationToken); return cacheValue; } diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs index 2ad61691..cfa87be8 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using Microsoft.Extensions.Logging; @@ -18,9 +19,9 @@ public partial class DefaultInMemoryCachingProvider : EasyCachingAbstractProvide /// Cache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, - TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -44,7 +45,7 @@ public override async Task> BaseGetAsync(string cacheKey, Func< if (!_cache.Add($"{cacheKey}_Lock", 1, TimeSpan.FromMilliseconds(_options.LockMs))) { //wait for some ms - await Task.Delay(_options.SleepMs); + await Task.Delay(_options.SleepMs, cancellationToken); return await GetAsync(cacheKey, dataRetriever, expiration); } @@ -80,8 +81,9 @@ public override async Task> BaseGetAsync(string cacheKey, Func< /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -112,7 +114,8 @@ public override async Task> BaseGetAsync(string cacheKey) /// /// The count. /// Prefix. - public override Task BaseGetCountAsync(string prefix = "") + /// CancellationToken + public override Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { return Task.FromResult(_cache.GetCount(prefix)); } @@ -123,7 +126,8 @@ public override Task BaseGetCountAsync(string prefix = "") /// The async. /// Cache key. /// Object Type. - public override async Task BaseGetAsync(string cacheKey, Type type) + /// CancellationToken + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -154,12 +158,13 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// /// The async. /// Cache key. - public override async Task BaseRemoveAsync(string cacheKey) + /// CancellationToken + public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await Task.Run(() => { _cache.Remove(cacheKey); }); - } + await Task.Run(() => { _cache.Remove(cacheKey); }, cancellationToken); + } /// /// Sets the specified cacheKey, cacheValue and expiration async. @@ -168,8 +173,9 @@ public override async Task BaseRemoveAsync(string cacheKey) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -186,7 +192,7 @@ 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); } /// @@ -194,41 +200,44 @@ await Task.Run(() => /// /// The async. /// Cache key. - public override async Task BaseExistsAsync(string cacheKey) + /// CancellationToken + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); return await Task.FromResult(_cache.Exists(cacheKey)); - } + } /// /// Removes cached item by cachekey's prefix async. /// /// The by prefix async. /// Prefix. - public override async Task BaseRemoveByPrefixAsync(string prefix) + /// CancellationToken + public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - var count = await Task.Run(() => _cache.RemoveByPrefix(prefix)); + var count = await Task.Run(() => _cache.RemoveByPrefix(prefix), cancellationToken); if (_options.EnableLogging) _logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix} , count = {count}"); } - + /// /// Sets all async. /// /// The all async. /// Values. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override async 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)); + await Task.Run(() => _cache.SetAll(values, expiration), cancellationToken); } /// @@ -236,8 +245,9 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -246,14 +256,15 @@ public override async Task>> BaseGetAllAsync(cacheKeys)); } - + /// /// Gets the by prefix async. /// /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix) + public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); var map = new Dictionary>(); @@ -269,21 +280,23 @@ public override async Task>> BaseGetByPrefixAs /// /// The all async. /// Cache keys. - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public override async 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)); + await Task.Run(() => _cache.RemoveAll(cacheKeys), cancellationToken); } /// /// Flush All Cached Item async. /// /// The async. - public override async Task BaseFlushAsync() + /// CancellationToken + public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("FlushAsync"); @@ -299,8 +312,9 @@ public override async Task BaseFlushAsync() /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -314,8 +328,9 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim /// Get the expiration of cache key /// /// cache key + /// CancellationToken /// expiration - public override Task BaseGetExpirationAsync(string cacheKey) + public override Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); diff --git a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.Async.cs b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.Async.cs index a14a0997..9e866ce6 100644 --- a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.Async.cs +++ b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.Async.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; /// @@ -16,11 +17,12 @@ public partial class DefaultLiteDBCachingProvider : EasyCachingAbstractProvider /// /// The async. /// Cache key. - public override async Task BaseExistsAsync(string cacheKey) + /// CancellationToken + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.Run(() => BaseExists(cacheKey)); + return await Task.Run(() => BaseExists(cacheKey), cancellationToken); } /// @@ -30,8 +32,9 @@ public override async Task BaseExistsAsync(string cacheKey) /// Cache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -41,7 +44,7 @@ public override async Task> BaseGetAsync(string cacheKey, Func< { return dataRetriever.Invoke().GetAwaiter().GetResult(); }, expiration); - }); + }, cancellationToken); } /// @@ -49,11 +52,12 @@ public override async Task> BaseGetAsync(string cacheKey, Func< /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.Run(() => BaseGet(cacheKey)); + return await Task.Run(() => BaseGet(cacheKey), cancellationToken); } /// @@ -61,9 +65,10 @@ public override async Task> BaseGetAsync(string cacheKey) /// /// The count. /// Prefix. - public override async Task BaseGetCountAsync(string prefix = "") + /// CancellationToken + public override async Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { - return await Task.Run(() => BaseGetCount(prefix)); + return await Task.Run(() => BaseGetCount(prefix), cancellationToken); } /// @@ -72,10 +77,11 @@ public override async Task BaseGetCountAsync(string prefix = "") /// The async. /// Cache key. /// Object Type. - public override async Task BaseGetAsync(string cacheKey, Type type) + /// CancellationToken + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.Run(() => System.Convert.ChangeType(BaseGet(cacheKey).Value, type)); + return await Task.Run(() => System.Convert.ChangeType(BaseGet(cacheKey).Value, type), cancellationToken); } /// @@ -83,11 +89,12 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// /// The async. /// Cache key. - public override async Task BaseRemoveAsync(string cacheKey) + /// CancellationToken + public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await Task.Run(() => BaseRemove(cacheKey)); + await Task.Run(() => BaseRemove(cacheKey), cancellationToken); } /// @@ -97,27 +104,29 @@ public override async Task BaseRemoveAsync(string cacheKey) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - await Task.Run(() => BaseSet(cacheKey, cacheValue, expiration)); + await Task.Run(() => BaseSet(cacheKey, cacheValue, expiration), cancellationToken); } /// /// Removes cached item by cachekey's prefix async. /// /// Prefix of CacheKey. - public override async Task BaseRemoveByPrefixAsync(string prefix) + /// CancellationToken + public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); if (_options.EnableLogging) _logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix}"); - await Task.Run(() => BaseRemoveByPrefix(prefix)); + await Task.Run(() => BaseRemoveByPrefix(prefix), cancellationToken); } /// @@ -126,10 +135,11 @@ public override async Task BaseRemoveByPrefixAsync(string prefix) /// The all async. /// Values. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { - await Task.Run(() => BaseSetAll(values, expiration)); + await Task.Run(() => BaseSetAll(values, expiration), cancellationToken); } /// @@ -137,11 +147,12 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - return await Task.Run(() => BaseGetAll(cacheKeys)); + return await Task.Run(() => BaseGetAll(cacheKeys), cancellationToken); } /// @@ -149,12 +160,13 @@ public override async Task>> BaseGetAllAsync /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix) + public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - return await Task.Run(() => BaseGetByPrefix(prefix)); + return await Task.Run(() => BaseGetByPrefix(prefix), cancellationToken); } /// @@ -162,17 +174,19 @@ public override async Task>> BaseGetByPrefixAs /// /// The all async. /// Cache keys. - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - await Task.Run(() => BaseRemoveAll(cacheKeys)); + await Task.Run(() => BaseRemoveAll(cacheKeys), cancellationToken); } /// /// Flush All Cached Item async. /// /// The async. - public override async Task BaseFlushAsync() => await Task.Run(BaseFlush); + /// CancellationToken + public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) => await Task.Run(BaseFlush, cancellationToken); /// /// Tries the set async. @@ -181,24 +195,26 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - return await Task.Run(() => BaseTrySet(cacheKey, cacheValue, expiration)); + return await Task.Run(() => BaseTrySet(cacheKey, cacheValue, expiration), cancellationToken); } /// /// Get the expiration of cache key async /// /// cache key + /// CancellationToken /// expiration - public override async Task BaseGetExpirationAsync(string cacheKey) + public override async Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.Run(() => BaseGetExpiration(cacheKey)); + return await Task.Run(() => BaseGetExpiration(cacheKey), cancellationToken); } } } \ No newline at end of file diff --git a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs index 71cced65..6354aa87 100644 --- a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs +++ b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; + using System.Threading; using System.Threading.Tasks; using Dapper; using EasyCaching.Core; @@ -18,15 +19,16 @@ public partial class DefaultSQLiteCachingProvider : EasyCachingAbstractProvider /// /// The async. /// Cache key. - public override async Task BaseExistsAsync(string cacheKey) + /// CancellationToken + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var dbResult = await _cache.ExecuteScalarAsync(ConstSQL.EXISTSSQL, new + var dbResult = await _cache.ExecuteScalarAsync(new CommandDefinition(ConstSQL.EXISTSSQL, new { cachekey = cacheKey, name = _name - }); + }, cancellationToken: cancellationToken)); return dbResult == 1; } @@ -38,17 +40,18 @@ public override async Task BaseExistsAsync(string cacheKey) /// Cache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - var list = await _cache.QueryAsync(ConstSQL.GETSQL, new + var list = await _cache.QueryAsync(new CommandDefinition(ConstSQL.GETSQL, new { cachekey = cacheKey, name = _name - }); + }, cancellationToken: cancellationToken)); var dbResult = list.FirstOrDefault(); @@ -85,16 +88,17 @@ public override async Task> BaseGetAsync(string cacheKey, Func< /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var list = await _cache.QueryAsync(ConstSQL.GETSQL, new + var list = await _cache.QueryAsync(new CommandDefinition(ConstSQL.GETSQL, new { cachekey = cacheKey, name = _name - }); + }, cancellationToken: cancellationToken)); var dbResult = list.FirstOrDefault(); @@ -123,15 +127,17 @@ public override async Task> BaseGetAsync(string cacheKey) /// /// The count. /// Prefix. - public override async Task BaseGetCountAsync(string prefix = "") + /// CancellationToken + public override async Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(prefix)) { - return await _cache.ExecuteScalarAsync(ConstSQL.COUNTALLSQL, new { name = _name }); + + return await _cache.ExecuteScalarAsync(new CommandDefinition(ConstSQL.COUNTALLSQL, new { name = _name }, cancellationToken: cancellationToken)); } else { - return await _cache.ExecuteScalarAsync(ConstSQL.COUNTPREFIXSQL, new { cachekey = string.Concat(prefix, "%"), name = _name }); + return await _cache.ExecuteScalarAsync(new CommandDefinition(ConstSQL.COUNTPREFIXSQL, new { cachekey = string.Concat(prefix, "%"), name = _name }, cancellationToken: cancellationToken)); } } @@ -141,15 +147,18 @@ public override async Task BaseGetCountAsync(string prefix = "") /// The async. /// Cache key. /// Object Type. - public override async Task BaseGetAsync(string cacheKey, Type type) + /// CancellationToken + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var list = await _cache.QueryAsync(ConstSQL.GETSQL, new + + + var list = await _cache.QueryAsync(new CommandDefinition(ConstSQL.GETSQL, new { cachekey = cacheKey, name = _name - }); + }, cancellationToken: cancellationToken)); var dbResult = list.FirstOrDefault(); @@ -178,11 +187,12 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// /// The async. /// Cache key. - public override async Task BaseRemoveAsync(string cacheKey) + /// CancellationToken + public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await _cache.ExecuteAsync(ConstSQL.REMOVESQL, new { cachekey = cacheKey, name = _name }); + await _cache.ExecuteAsync(new CommandDefinition(ConstSQL.REMOVESQL, new { cachekey = cacheKey, name = _name }, cancellationToken: cancellationToken)); } /// @@ -192,8 +202,9 @@ public override async Task BaseRemoveAsync(string cacheKey) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -205,37 +216,43 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp expiration.Add(new TimeSpan(0, 0, addSec)); } - await _cache.ExecuteAsync(ConstSQL.SETSQL, new + await _cache.ExecuteAsync(new CommandDefinition(ConstSQL.SETSQL, new { cachekey = cacheKey, name = _name, cachevalue = Newtonsoft.Json.JsonConvert.SerializeObject(cacheValue), expiration = expiration.Ticks / 10000000 - }); - } - + }, cancellationToken: cancellationToken)); + } + /// /// Removes cached item by cachekey's prefix async. /// /// Prefix of CacheKey. - public override async Task BaseRemoveByPrefixAsync(string prefix) + /// CancellationToken + public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); if (_options.EnableLogging) _logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix}"); - await _cache.ExecuteAsync(ConstSQL.REMOVEBYPREFIXSQL, new { cachekey = string.Concat(prefix, "%"), name = _name }); + await _cache.ExecuteAsync(new CommandDefinition(ConstSQL.REMOVEBYPREFIXSQL, new + { + cachekey = string.Concat(prefix, "%"), + name = _name + }, cancellationToken: cancellationToken)); } - + /// /// Sets all async. /// /// The all async. /// Values. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); ArgumentCheck.NotNullAndCountGTZero(values, nameof(values)); @@ -257,22 +274,23 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim tran.Commit(); } - + /// /// Gets all async. /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); - var list = (await _cache.QueryAsync(ConstSQL.GETALLSQL, new + var list = (await _cache.QueryAsync(new CommandDefinition(ConstSQL.GETALLSQL, new { cachekey = cacheKeys.ToArray(), name = _name - })).ToList(); + }, cancellationToken: cancellationToken))).ToList(); return GetDict(list); } @@ -282,26 +300,28 @@ public override async Task>> BaseGetAllAsync /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix) + public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - var list = (await _cache.QueryAsync(ConstSQL.GETBYPREFIXSQL, new + var list = (await _cache.QueryAsync(new CommandDefinition(ConstSQL.GETBYPREFIXSQL, new { cachekey = string.Concat(prefix, "%"), name = _name - })).ToList(); + }, cancellationToken: cancellationToken))).ToList(); return GetDict(list); } - + /// /// Removes all async. /// /// The all async. /// Cache keys. - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -318,8 +338,9 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) /// /// Flush All Cached Item async. /// + /// CancellationToken /// The async. - public override async Task BaseFlushAsync() => await _cache.ExecuteAsync(ConstSQL.FLUSHSQL, new { name = _name }); + public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) => await _cache.ExecuteAsync(new CommandDefinition(ConstSQL.FLUSHSQL, new { name = _name }, cancellationToken: cancellationToken)); /// /// Tries the set async. @@ -328,8 +349,9 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -341,13 +363,13 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu expiration.Add(new TimeSpan(0, 0, addSec)); } - var rows = await _cache.ExecuteAsync(ConstSQL.TRYSETSQL, new + var rows = await _cache.ExecuteAsync(new CommandDefinition(ConstSQL.TRYSETSQL, new { cachekey = cacheKey, name = _name, cachevalue = Newtonsoft.Json.JsonConvert.SerializeObject(cacheValue), expiration = expiration.Ticks / 10000000 - }); + }, cancellationToken: cancellationToken)); return rows > 0; } @@ -356,16 +378,17 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu /// Get the expiration of cache key async /// /// cache key + /// CancellationToken /// expiration - public override async Task BaseGetExpirationAsync(string cacheKey) + public override async Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - var time = await _cache.ExecuteScalarAsync(ConstSQL.GETEXPIRATIONSQL, new + var time = await _cache.ExecuteScalarAsync(new CommandDefinition(ConstSQL.GETEXPIRATIONSQL, new { cachekey = cacheKey, name = _name - }); + }, cancellationToken: cancellationToken)); if (time <= 0) return TimeSpan.Zero; else return TimeSpan.FromSeconds(time); From bd164b7cf62c228c164610c210ab87af3fe066d3 Mon Sep 17 00:00:00 2001 From: st0o0 Date: Tue, 2 Aug 2022 11:22:20 +0200 Subject: [PATCH 2/5] finished CancellationToken implementation --- .../DefaultMemcachedCachingProvider.Async.cs | 69 +++++++++++-------- .../DefaultRedisCachingProvider.Async.cs | 51 +++++++++----- 2 files changed, 75 insertions(+), 45 deletions(-) diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs index 600ace27..5ab09d71 100644 --- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs +++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using Microsoft.Extensions.Logging; @@ -11,7 +12,7 @@ /// Default memcached caching provider. /// public partial class DefaultMemcachedCachingProvider : EasyCachingAbstractProvider - { + { /// /// Gets the specified cacheKey, dataRetriever and expiration async. /// @@ -19,13 +20,14 @@ public partial class DefaultMemcachedCachingProvider : EasyCachingAbstractProvid /// Cache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); - var result = await BaseGetAsync(cacheKey); + var result = await BaseGetAsync(cacheKey, cancellationToken); if (result.HasValue) { @@ -38,13 +40,13 @@ public override async Task> BaseGetAsync(string cacheKey, Func< if (!flag) { await Task.Delay(_options.SleepMs); - return await GetAsync(cacheKey, dataRetriever, expiration); + return await GetAsync(cacheKey, dataRetriever, expiration, cancellationToken); } var item = await dataRetriever(); if (item != null || _options.CacheNulls) { - await this.SetAsync(cacheKey, item, expiration); + await this.SetAsync(cacheKey, item, expiration, cancellationToken); await _memcachedClient.RemoveAsync(this.HandleCacheKey($"{cacheKey}_Lock")); return new CacheValue(item, true); } @@ -54,14 +56,15 @@ public override async Task> BaseGetAsync(string cacheKey, Func< return CacheValue.NoValue; } } - + /// /// Gets the specified cacheKey async. /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -88,7 +91,8 @@ public override async Task> BaseGetAsync(string cacheKey) /// /// The count. /// Prefix. - public override Task BaseGetCountAsync(string prefix = "") + /// CancellationToken + public override Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(prefix)) { @@ -107,7 +111,8 @@ public override Task BaseGetCountAsync(string prefix = "") /// The async. /// Cache key. /// Object Type. - public override async Task BaseGetAsync(string cacheKey, Type type) + /// CancellationToken + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -129,7 +134,8 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// /// The async. /// Cache key. - public override async Task BaseRemoveAsync(string cacheKey) + /// CancellationToken + public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -144,8 +150,9 @@ public override async Task BaseRemoveAsync(string cacheKey) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -169,13 +176,14 @@ await _memcachedClient.StoreAsync( /// /// The async. /// Cache key. - public override async Task BaseExistsAsync(string cacheKey) + /// CancellationToken + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); return await Task.FromResult(_memcachedClient.TryGet(this.HandleCacheKey(cacheKey), out object obj)); } - + /// /// Removes cached item by cachekey's prefix async. /// @@ -185,8 +193,9 @@ public override async Task BaseExistsAsync(string cacheKey) /// and confirm that you use the namespacing when you set and get the cache. /// /// Prefix of CacheKey. + /// CancellationToken /// - public override async Task BaseRemoveByPrefixAsync(string prefix) + public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -215,8 +224,9 @@ await _memcachedClient.StoreAsync( /// The all async. /// Values. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); ArgumentCheck.NotNullAndCountGTZero(values, nameof(values)); @@ -224,18 +234,19 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim var tasks = new List(); foreach (var item in values) { - tasks.Add(SetAsync(item.Key, item.Value, expiration)); + tasks.Add(SetAsync(item.Key, item.Value, expiration, cancellationToken)); } await Task.WhenAll(tasks); } - + /// /// Gets all async. /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -246,30 +257,32 @@ public override async Task>> BaseGetAllAsync pair.Key, pair => ConvertFromStoredValue(pair.Value)); } - + /// /// Gets the by prefix async. /// /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - public override Task>> BaseGetByPrefixAsync(string prefix) + public override Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } - + /// /// Removes all async. /// /// The all async. /// Cache keys. - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); var tasks = new List(); foreach (var item in cacheKeys.Distinct()) - tasks.Add(RemoveAsync(item)); + tasks.Add(RemoveAsync(item, cancellationToken)); await Task.WhenAll(tasks); } @@ -278,14 +291,15 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) /// Flush All Cached Item async. /// /// The async. - public override async Task BaseFlushAsync() + /// CancellationToken + public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("Memcached -- FlushAsync"); await _memcachedClient.FlushAllAsync(); } - + /// /// Tries the set async. /// @@ -293,8 +307,9 @@ public override async Task BaseFlushAsync() /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -313,7 +328,7 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim expiration); } - public override Task BaseGetExpirationAsync(string cacheKey) + public override Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs index 89a7d137..3fffbfdb 100644 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; using Microsoft.Extensions.Logging; @@ -19,7 +20,8 @@ public partial class DefaultRedisCachingProvider : EasyCachingAbstractProvider /// The async. /// Cache key. /// Object Type. - public override async Task BaseGetAsync(string cacheKey, Type type) + /// CancellationToken + public override async Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -52,8 +54,9 @@ public override async Task BaseGetAsync(string cacheKey, Type type) /// Cache key. /// Data retriever. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override async Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); @@ -79,8 +82,8 @@ public override async Task> BaseGetAsync(string cacheKey, Func< if (!flag) { - await Task.Delay(_options.SleepMs); - return await GetAsync(cacheKey, dataRetriever, expiration); + await Task.Delay(_options.SleepMs, cancellationToken); + return await GetAsync(cacheKey, dataRetriever, expiration, cancellationToken); } var item = await dataRetriever(); @@ -105,8 +108,9 @@ public override async Task> BaseGetAsync(string cacheKey, Func< /// /// The async. /// Cache key. + /// CancellationToken /// The 1st type parameter. - public override async Task> BaseGetAsync(string cacheKey) + public override async Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -137,7 +141,8 @@ public override async Task> BaseGetAsync(string cacheKey) /// /// The count. /// Prefix. - public override Task BaseGetCountAsync(string prefix = "") + /// CancellationToken + public override Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(prefix)) { @@ -157,7 +162,8 @@ public override Task BaseGetCountAsync(string prefix = "") /// /// The async. /// Cache key. - public override async Task BaseRemoveAsync(string cacheKey) + /// CancellationToken + public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -171,8 +177,9 @@ public override async Task BaseRemoveAsync(string cacheKey) /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -195,7 +202,8 @@ await _cache.StringSetAsync( /// /// The async. /// Cache key. - public override async Task BaseExistsAsync(string cacheKey) + /// CancellationToken + public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); @@ -206,7 +214,8 @@ public override async Task BaseExistsAsync(string cacheKey) /// Removes cached item by cachekey's prefix async. /// /// Prefix of CacheKey. - public override async Task BaseRemoveByPrefixAsync(string prefix) + /// CancellationToken + public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -226,8 +235,9 @@ public override async Task BaseRemoveByPrefixAsync(string prefix) /// The all async. /// Values. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); ArgumentCheck.NotNullAndCountGTZero(values, nameof(values)); @@ -235,7 +245,7 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim var tasks = new List(); foreach (var item in values) - tasks.Add(SetAsync(item.Key, item.Value, expiration)); + tasks.Add(SetAsync(item.Key, item.Value, expiration, cancellationToken)); await Task.WhenAll(tasks); } @@ -245,8 +255,9 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim /// /// The all async. /// Cache keys. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys) + public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -271,8 +282,9 @@ public override async Task>> BaseGetAllAsync /// The by prefix async. /// Prefix. + /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix) + public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); @@ -300,7 +312,8 @@ public override async Task>> BaseGetByPrefixAs /// /// The all async. /// Cache keys. - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) + /// CancellationToken + public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); @@ -313,7 +326,7 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys) /// Flush All Cached Item async. /// /// The async. - public override async Task BaseFlushAsync() + public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("Redis -- FlushAsync"); @@ -335,8 +348,9 @@ public override async Task BaseFlushAsync() /// Cache key. /// Cache value. /// Expiration. + /// CancellationToken /// The 1st type parameter. - public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -360,8 +374,9 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim /// Get the expiration of cache key /// /// cache key + /// CancellationToken /// expiration - public override async Task BaseGetExpirationAsync(string cacheKey) + public override async Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); From 1b5304d646f49ed6500fe4d725bbe2ebb89c7d88 Mon Sep 17 00:00:00 2001 From: st0o0 Date: Tue, 2 Aug 2022 18:52:56 +0200 Subject: [PATCH 3/5] bugfixes Unittest --- .../Diagnostics/MyCachingProvider.cs | 33 ++++++++--------- .../EasyCachingAbstractProviderTest.cs | 3 +- .../Fake/FakeDistributedCachingProvider.cs | 35 ++++++++++--------- .../Fake/FakeLocalCachingProvider.cs | 35 ++++++++++--------- 4 files changed, 55 insertions(+), 51 deletions(-) diff --git a/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs b/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs index 59c3165d..698d10f7 100644 --- a/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs +++ b/test/EasyCaching.UnitTests/Diagnostics/MyCachingProvider.cs @@ -2,7 +2,8 @@ { using EasyCaching.Core; using System; - using System.Collections.Generic; + using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; public class MyCachingProvider : EasyCachingAbstractProvider @@ -21,7 +22,7 @@ public override bool BaseExists(string cacheKey) return true; } - public override Task BaseExistsAsync(string cacheKey) + public override Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(false); } @@ -31,7 +32,7 @@ public override void BaseFlush() } - public override Task BaseFlushAsync() + public override Task BaseFlushAsync(CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -51,22 +52,22 @@ public override IDictionary> BaseGetAll(IEnumerable>> BaseGetAllAsync(IEnumerable cacheKeys) + public override Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { return null; } - public override Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.FromResult(CacheValue.NoValue); } - public override Task BaseGetAsync(string cacheKey, Type type) + public override Task BaseGetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { return Task.FromResult(null); } - public override Task> BaseGetAsync(string cacheKey) + public override Task> BaseGetAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(CacheValue.NoValue); } @@ -76,7 +77,7 @@ public override IDictionary> BaseGetByPrefix(string pre return null; } - public override Task>> BaseGetByPrefixAsync(string prefix) + public override Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { return Task.FromResult>>(null); } @@ -86,7 +87,7 @@ public override int BaseGetCount(string prefix = "") return 1; } - public override Task BaseGetCountAsync(string prefix = "") + public override Task BaseGetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { return Task.FromResult(1); } @@ -96,7 +97,7 @@ public override TimeSpan BaseGetExpiration(string cacheKey) return TimeSpan.FromSeconds(1); } - public override Task BaseGetExpirationAsync(string cacheKey) + public override Task BaseGetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(TimeSpan.FromSeconds(1)); } @@ -116,12 +117,12 @@ public override void BaseRemoveAll(IEnumerable cacheKeys) } - public override Task BaseRemoveAllAsync(IEnumerable cacheKeys) + public override Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { return Task.CompletedTask; } - public override Task BaseRemoveAsync(string cacheKey) + public override Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -131,7 +132,7 @@ public override void BaseRemoveByPrefix(string prefix) } - public override Task BaseRemoveByPrefixAsync(string prefix) + public override Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -146,12 +147,12 @@ public override void BaseSetAll(IDictionary values, TimeSpan expir } - public override Task BaseSetAllAsync(IDictionary values, TimeSpan expiration) + public override Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } - public override Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -161,7 +162,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir return false; } - public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public override Task BaseTrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.FromResult(false); } diff --git a/test/EasyCaching.UnitTests/DistributedLock/EasyCachingAbstractProviderTest.cs b/test/EasyCaching.UnitTests/DistributedLock/EasyCachingAbstractProviderTest.cs index 158a5e5a..1ae207db 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/EasyCachingAbstractProviderTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/EasyCachingAbstractProviderTest.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Xunit; @@ -55,7 +56,7 @@ public override CacheValue BaseGet(string cacheKey, Func dataRetriever, throw new InvalidOperationException(); } - public override Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public override Task> BaseGetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { throw new InvalidOperationException(); } diff --git a/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs b/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs index a5927505..8d350618 100644 --- a/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs +++ b/test/EasyCaching.UnitTests/Fake/FakeDistributedCachingProvider.cs @@ -1,7 +1,8 @@ namespace EasyCaching.UnitTests { using System; - using System.Collections.Generic; + using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; @@ -28,7 +29,7 @@ public virtual bool Exists(string cacheKey) return true; } - public virtual Task ExistsAsync(string cacheKey) + public virtual Task ExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(true); } @@ -38,7 +39,7 @@ public virtual void Flush() } - public virtual Task FlushAsync() + public virtual Task FlushAsync(CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -58,23 +59,23 @@ public virtual IDictionary> GetAll(IEnumerable return new Dictionary>(); } - public virtual Task>> GetAllAsync(IEnumerable cacheKeys) + public virtual Task>> GetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { IDictionary> dict = new Dictionary>(); return Task.FromResult(dict); } - public virtual Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public virtual Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.FromResult(new CacheValue(default(T), true)); } - public virtual Task GetAsync(string cacheKey, Type type) + public virtual Task GetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { return Task.FromResult(null); } - public virtual Task> GetAsync(string cacheKey) + public virtual Task> GetAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(new CacheValue(default(T), true)); } @@ -84,7 +85,7 @@ public virtual IDictionary> GetByPrefix(string prefix) return new Dictionary>(); } - public virtual Task>> GetByPrefixAsync(string prefix) + public virtual Task>> GetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { IDictionary> dict = new Dictionary>(); return Task.FromResult(dict); @@ -95,7 +96,7 @@ public virtual int GetCount(string prefix = "") return 1; } - public Task GetCountAsync(string prefix = "") + public Task GetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { return Task.FromResult(1); } @@ -105,7 +106,7 @@ public virtual TimeSpan GetExpiration(string cacheKey) return TimeSpan.FromSeconds(1); } - public virtual Task GetExpirationAsync(string cacheKey) + public virtual Task GetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(TimeSpan.FromSeconds(1)); } @@ -120,7 +121,7 @@ public virtual void Refresh(string cacheKey, T cacheValue, TimeSpan expiratio } - public virtual Task RefreshAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public virtual Task RefreshAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -135,12 +136,12 @@ public virtual void RemoveAll(IEnumerable cacheKeys) } - public virtual Task RemoveAllAsync(IEnumerable cacheKeys) + public virtual Task RemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { return Task.CompletedTask; } - public virtual Task RemoveAsync(string cacheKey) + public virtual Task RemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -150,7 +151,7 @@ public virtual void RemoveByPrefix(string prefix) } - public virtual Task RemoveByPrefixAsync(string prefix) + public virtual Task RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -165,12 +166,12 @@ public virtual void SetAll(IDictionary value, TimeSpan expiration) } - public virtual Task SetAllAsync(IDictionary value, TimeSpan expiration) + public virtual Task SetAllAsync(IDictionary value, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } - public virtual Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public virtual Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -180,7 +181,7 @@ public virtual bool TrySet(string cacheKey, T cacheValue, TimeSpan expiration return true; } - public virtual Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public virtual Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.FromResult(true); } diff --git a/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs b/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs index f1fa863a..a2bead29 100644 --- a/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs +++ b/test/EasyCaching.UnitTests/Fake/FakeLocalCachingProvider.cs @@ -1,7 +1,8 @@ namespace EasyCaching.UnitTests { using System; - using System.Collections.Generic; + using System.Collections.Generic; + using System.Threading; using System.Threading.Tasks; using EasyCaching.Core; @@ -28,7 +29,7 @@ public bool Exists(string cacheKey) return true; } - public Task ExistsAsync(string cacheKey) + public Task ExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(true); } @@ -38,7 +39,7 @@ public void Flush() } - public Task FlushAsync() + public Task FlushAsync(CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -58,23 +59,23 @@ public IDictionary> GetAll(IEnumerable cacheKey return new Dictionary>(); } - public Task>> GetAllAsync(IEnumerable cacheKeys) + public Task>> GetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { IDictionary> dict = new Dictionary>(); return Task.FromResult(dict); } - public Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration) + public Task> GetAsync(string cacheKey, Func> dataRetriever, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.FromResult(new CacheValue(default(T), true)); } - public Task GetAsync(string cacheKey, Type type) + public Task GetAsync(string cacheKey, Type type, CancellationToken cancellationToken = default) { return Task.FromResult(null); } - public Task> GetAsync(string cacheKey) + public Task> GetAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(new CacheValue(default(T), true)); } @@ -84,7 +85,7 @@ public IDictionary> GetByPrefix(string prefix) return new Dictionary>(); } - public Task>> GetByPrefixAsync(string prefix) + public Task>> GetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { IDictionary> dict = new Dictionary>(); return Task.FromResult(dict); @@ -95,7 +96,7 @@ public int GetCount(string prefix = "") return 1; } - public Task GetCountAsync(string prefix = "") + public Task GetCountAsync(string prefix = "", CancellationToken cancellationToken = default) { return Task.FromResult(1); } @@ -105,7 +106,7 @@ public TimeSpan GetExpiration(string cacheKey) return TimeSpan.FromSeconds(1); } - public Task GetExpirationAsync(string cacheKey) + public Task GetExpirationAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.FromResult(TimeSpan.FromSeconds(1)); } @@ -120,7 +121,7 @@ public void Refresh(string cacheKey, T cacheValue, TimeSpan expiration) } - public Task RefreshAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public Task RefreshAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -135,12 +136,12 @@ public void RemoveAll(IEnumerable cacheKeys) } - public Task RemoveAllAsync(IEnumerable cacheKeys) + public Task RemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { return Task.CompletedTask; } - public Task RemoveAsync(string cacheKey) + public Task RemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -150,7 +151,7 @@ public void RemoveByPrefix(string prefix) } - public Task RemoveByPrefixAsync(string prefix) + public Task RemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -165,12 +166,12 @@ public void SetAll(IDictionary value, TimeSpan expiration) } - public Task SetAllAsync(IDictionary value, TimeSpan expiration) + public Task SetAllAsync(IDictionary value, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } - public Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public Task SetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.CompletedTask; } @@ -180,7 +181,7 @@ public bool TrySet(string cacheKey, T cacheValue, TimeSpan expiration) return true; } - public Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration) + public Task TrySetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { return Task.FromResult(true); } From 04d2bed08f9d70c606585b7172805c7fc3240ead Mon Sep 17 00:00:00 2001 From: st0o0 Date: Tue, 2 Aug 2022 18:59:02 +0200 Subject: [PATCH 4/5] HybridCachingTest needed a default CancellationToken --- test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs b/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs index 1cb389ff..b4170b95 100644 --- a/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/HybridCachingTest.cs @@ -185,7 +185,7 @@ public void Distributed_Remove_Throw_Exception_Should_Not_Break() [Fact] public async Task Distributed_Remove_Async_Throw_Exception_Should_Not_Break() { - A.CallTo(() => fakeDisProvider.RemoveAsync("fake-remove-key")).ThrowsAsync(new Exception()); + A.CallTo(() => fakeDisProvider.RemoveAsync("fake-remove-key", default)).ThrowsAsync(new Exception()); await fakeHybrid.RemoveAsync("fake-remove-key"); @@ -210,7 +210,7 @@ public void Distributed_Set_Throw_Exception_Should_Not_Break() [Fact] public async Task Distributed_Set_Async_Throw_Exception_Should_Not_Break() { - A.CallTo(() => fakeDisProvider.SetAsync(A.Ignored, A.Ignored, A.Ignored)).ThrowsAsync(new Exception()); + A.CallTo(() => fakeDisProvider.SetAsync(A.Ignored, A.Ignored, A.Ignored, default)).ThrowsAsync(new Exception()); var key = $"d-set-{Guid.NewGuid().ToString()}"; From df12b51d484fbd9bd19305ef71070a2a4e7b4d5e Mon Sep 17 00:00:00 2001 From: st0o0 Date: Wed, 3 Aug 2022 16:08:29 +0200 Subject: [PATCH 5/5] XML Comment removed --- src/EasyCaching.HybridCache/HybridCachingProvider.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/EasyCaching.HybridCache/HybridCachingProvider.cs b/src/EasyCaching.HybridCache/HybridCachingProvider.cs index 55a41839..92f7ab79 100644 --- a/src/EasyCaching.HybridCache/HybridCachingProvider.cs +++ b/src/EasyCaching.HybridCache/HybridCachingProvider.cs @@ -188,7 +188,7 @@ public async Task ExistsAsync(string cacheKey, CancellationToken cancellat // Circuit Breaker may be more better try { - flag = await _distributedCache.ExistsAsync(cacheKey); + flag = await _distributedCache.ExistsAsync(cacheKey, cancellationToken); return flag; } catch (Exception ex) @@ -196,7 +196,7 @@ public async Task ExistsAsync(string cacheKey, CancellationToken cancellat LogMessage($"Check cache key [{cacheKey}] exists error", ex); } - flag = await _localCache.ExistsAsync(cacheKey); + flag = await _localCache.ExistsAsync(cacheKey, cancellationToken); return flag; } @@ -205,7 +205,6 @@ public async Task ExistsAsync(string cacheKey, CancellationToken cancellat /// /// The get. /// Cache key. - /// CancellationToken /// The 1st type parameter. public CacheValue Get(string cacheKey) {