diff --git a/src/EasyCaching.Core/DistributedLock/RefCounter.cs b/src/EasyCaching.Core/DistributedLock/RefCounter.cs deleted file mode 100644 index d5f8c189..00000000 --- a/src/EasyCaching.Core/DistributedLock/RefCounter.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Threading; - -namespace EasyCaching.Core.DistributedLock -{ - internal class RefCounter - { - private int _refCount = 1; - - public RefCounter(T value) => Value = value; - - public T Value { get; } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Increment() => Interlocked.Increment(ref _refCount); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Decrement() => Interlocked.Decrement(ref _refCount); - } - - internal class RefCounterPool where TValue : class - { - private readonly IDictionary> _dictionary; - - public RefCounterPool() => _dictionary = new Dictionary>(); - - public TValue GetOrAdd(TKey key, Func valueFactory) - { - if (valueFactory == null) throw new ArgumentNullException(nameof(valueFactory)); - - RefCounter item; - lock (_dictionary) - { - if (!_dictionary.TryGetValue(key, out item)) - return (_dictionary[key] = new RefCounter(valueFactory(key))).Value; - } - - item.Increment(); - - return item.Value; - } - - public TValue TryRemove(TKey key) - { - RefCounter item; - - lock (_dictionary) - { - if (!_dictionary.TryGetValue(key, out item) || item.Decrement() > 0) return null; - - _dictionary.Remove(key); - } - - return item.Value; - } - } -} diff --git a/src/EasyCaching.Core/EasyCachingAbstractBus.cs b/src/EasyCaching.Core/EasyCachingAbstractBus.cs index fc2179df..a98acc1e 100644 --- a/src/EasyCaching.Core/EasyCachingAbstractBus.cs +++ b/src/EasyCaching.Core/EasyCachingAbstractBus.cs @@ -28,53 +28,31 @@ public abstract class EasyCachingAbstractBus : IEasyCachingBus public void Publish(string topic, EasyCachingMessage message) { var operationId = s_diagnosticListener.WritePublishMessageBefore(new BeforePublishMessageRequestEventData(topic, message)); - Exception e = null; try { BasePublish(topic, message); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WritePublishMessageError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WritePublishMessageError(operationId, e); - } - else - { - s_diagnosticListener.WritePublishMessageAfter(operationId); - } - } + s_diagnosticListener.WritePublishMessageAfter(operationId); } public async Task PublishAsync(string topic, EasyCachingMessage message, CancellationToken cancellationToken = default(CancellationToken)) { var operationId = s_diagnosticListener.WritePublishMessageBefore(new BeforePublishMessageRequestEventData(topic, message)); - Exception e = null; try { await BasePublishAsync(topic, message, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WritePublishMessageError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WritePublishMessageError(operationId, e); - } - else - { - s_diagnosticListener.WritePublishMessageAfter(operationId); - } - } + s_diagnosticListener.WritePublishMessageAfter(operationId); } public void Subscribe(string topic, Action action, Action reconnectAction) @@ -94,27 +72,16 @@ public void Subscribe(string topic, Action action, Action re public virtual void BaseOnMessage(EasyCachingMessage message) { var operationId = s_diagnosticListener.WriteSubscribeMessageBefore(new BeforeSubscribeMessageRequestEventData(message)); - Exception e = null; try { _handler?.Invoke(message); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WritePublishMessageError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WritePublishMessageError(operationId, e); - } - else - { - s_diagnosticListener.WritePublishMessageAfter(operationId); - } - } + s_diagnosticListener.WritePublishMessageAfter(operationId); } public virtual void BaseOnReconnect() diff --git a/src/EasyCaching.Core/EasyCachingAbstractProvider.cs b/src/EasyCaching.Core/EasyCachingAbstractProvider.cs index b4985128..ba66968e 100644 --- a/src/EasyCaching.Core/EasyCachingAbstractProvider.cs +++ b/src/EasyCaching.Core/EasyCachingAbstractProvider.cs @@ -82,112 +82,71 @@ protected EasyCachingAbstractProvider(IDistributedLockFactory lockFactory, BaseP public bool Exists(string cacheKey) { var operationId = s_diagnosticListener.WriteExistsCacheBefore(new BeforeExistsRequestEventData(CachingProviderType.ToString(), Name, nameof(Exists), cacheKey)); - Exception e = null; + bool result; try { - return BaseExists(cacheKey); + result = BaseExists(cacheKey); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteExistsCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteExistsCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteExistsCacheAfter(operationId); - } - } + s_diagnosticListener.WriteExistsCacheAfter(operationId); + return result; } 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; + bool result; try { - var flag = await BaseExistsAsync(cacheKey, cancellationToken); - return flag; + result = await BaseExistsAsync(cacheKey, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteExistsCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteExistsCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteExistsCacheAfter(operationId); - } - } + s_diagnosticListener.WriteExistsCacheAfter(operationId); + return result; } public void Flush() { var operationId = s_diagnosticListener.WriteFlushCacheBefore(new EventData(CachingProviderType.ToString(), Name, nameof(Flush))); - Exception e = null; try { BaseFlush(); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteFlushCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteFlushCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteFlushCacheAfter(operationId); - } - } + s_diagnosticListener.WriteFlushCacheAfter(operationId); } 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(cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteFlushCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteFlushCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteFlushCacheAfter(operationId); - } - } + s_diagnosticListener.WriteFlushCacheAfter(operationId); } public CacheValue Get(string cacheKey, Func dataRetriever, TimeSpan expiration) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(Get), new[] { cacheKey }, expiration)); - Exception e = null; + bool doNotEnterFinally = false; try { if (_lockFactory == null) return BaseGet(cacheKey, dataRetriever, expiration); @@ -217,16 +176,13 @@ public CacheValue Get(string cacheKey, Func dataRetriever, TimeSpan exp } catch (Exception ex) { - e = ex; + doNotEnterFinally = true; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } finally { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else + if (!doNotEnterFinally) { s_diagnosticListener.WriteGetCacheAfter(operationId); } @@ -236,137 +192,92 @@ public CacheValue Get(string cacheKey, Func dataRetriever, TimeSpan exp public CacheValue Get(string cacheKey) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(Get), new[] { cacheKey })); - Exception e = null; + CacheValue result; try { - return BaseGet(cacheKey); + result = BaseGet(cacheKey); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } - + public IEnumerable GetAllKeysByPrefix(string prefix) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAllKeysByPrefix), new[] { prefix })); - Exception e = null; + IEnumerable result; try { - return BaseGetAllKeysByPrefix(prefix); + result = BaseGetAllKeysByPrefix(prefix); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } - + public async Task> GetAllKeysByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAllKeysByPrefixAsync), new[] { prefix })); - Exception e = null; + IEnumerable result; try { - return await BaseGetAllKeysByPrefixAsync(prefix); + result = await BaseGetAllKeysByPrefixAsync(prefix); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } public IDictionary> GetAll(IEnumerable cacheKeys) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetAll), cacheKeys.ToArray())); - Exception e = null; + IDictionary> result; try { - return BaseGetAll(cacheKeys); + result = BaseGetAll(cacheKeys); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } 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; + IDictionary> result; try { - return await BaseGetAllAsync(cacheKeys, cancellationToken); + result = await BaseGetAllAsync(cacheKeys, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } 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; + bool doNotEnterFinally = false; try { if (_lockFactory == null) return await BaseGetAsync(cacheKey, dataRetriever, expiration, cancellationToken); @@ -406,16 +317,13 @@ await Task.WhenAny(task, Task.Delay(_options.LockMs)) != task) } catch (Exception ex) { - e = ex; + doNotEnterFinally = true; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } finally { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else + if (!doNotEnterFinally) { s_diagnosticListener.WriteGetCacheAfter(operationId); } @@ -425,105 +333,69 @@ await Task.WhenAny(task, Task.Delay(_options.LockMs)) != task) 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; + object result; try { - return await BaseGetAsync(cacheKey, type, cancellationToken); + result = await BaseGetAsync(cacheKey, type, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } 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; + CacheValue result; try { - return await BaseGetAsync(cacheKey, cancellationToken); + result = await BaseGetAsync(cacheKey, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } public IDictionary> GetByPrefix(string prefix) { var operationId = s_diagnosticListener.WriteGetCacheBefore(new BeforeGetRequestEventData(CachingProviderType.ToString(), Name, nameof(GetByPrefix), new[] { prefix })); - Exception e = null; + IDictionary> result; try { - return BaseGetByPrefix(prefix); + result = BaseGetByPrefix(prefix); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } 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; + IDictionary> result; try { - return await BaseGetByPrefixAsync(prefix, cancellationToken); + result = await BaseGetByPrefixAsync(prefix, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteGetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteGetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteGetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteGetCacheAfter(operationId); + return result; } public int GetCount(string prefix = "") @@ -539,157 +411,91 @@ public async Task GetCountAsync(string prefix = "", CancellationToken cance public void Remove(string cacheKey) { var operationId = s_diagnosticListener.WriteRemoveCacheBefore(new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(Remove), new[] { cacheKey })); - Exception e = null; try { BaseRemove(cacheKey); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } public void RemoveAll(IEnumerable cacheKeys) { var operationId = s_diagnosticListener.WriteRemoveCacheBefore(new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveAll), cacheKeys.ToArray())); - Exception e = null; try { BaseRemoveAll(cacheKeys); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } 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, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } 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, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } public void RemoveByPrefix(string prefix) { var operationId = s_diagnosticListener.WriteRemoveCacheBefore(new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveByPrefix), new[] { prefix })); - Exception e = null; try { BaseRemoveByPrefix(prefix); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } 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, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } public void RemoveByPattern(string pattern) @@ -697,27 +503,16 @@ public void RemoveByPattern(string pattern) var operationId = s_diagnosticListener.WriteRemoveCacheBefore( new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveByPattern), new[] { pattern })); - Exception e = null; try { BaseRemoveByPattern(pattern); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } public async Task RemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default) @@ -725,183 +520,110 @@ public async Task RemoveByPatternAsync(string pattern, CancellationToken cancell var operationId = s_diagnosticListener.WriteRemoveCacheBefore( new BeforeRemoveRequestEventData(CachingProviderType.ToString(), Name, nameof(RemoveByPatternAsync), new[] { pattern })); - Exception e = null; try { await BaseRemoveByPatternAsync(pattern, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteRemoveCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteRemoveCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteRemoveCacheAfter(operationId); - } - } + s_diagnosticListener.WriteRemoveCacheAfter(operationId); } public void Set(string cacheKey, T cacheValue, TimeSpan expiration) { var operationId = s_diagnosticListener.WriteSetCacheBefore(new BeforeSetRequestEventData(CachingProviderType.ToString(), Name, nameof(Set), new Dictionary { { cacheKey, cacheValue } }, expiration)); - Exception e = null; try { BaseSet(cacheKey, cacheValue, expiration); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteSetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteSetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteSetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteSetCacheAfter(operationId); } public void SetAll(IDictionary value, TimeSpan expiration) { var operationId = s_diagnosticListener.WriteSetCacheBefore(new BeforeSetRequestEventData(CachingProviderType.ToString(), Name, nameof(SetAll), value.ToDictionary(k => k.Key, v => (object)v.Value), expiration)); - Exception e = null; try { BaseSetAll(value, expiration); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteSetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteSetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteSetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteSetCacheAfter(operationId); } 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, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteSetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteSetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteSetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteSetCacheAfter(operationId); } 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, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteSetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteSetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteSetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteSetCacheAfter(operationId); } public bool TrySet(string cacheKey, T cacheValue, TimeSpan expiration) { var operationId = s_diagnosticListener.WriteSetCacheBefore(new BeforeSetRequestEventData(CachingProviderType.ToString(), Name, nameof(TrySet), new Dictionary { { cacheKey, cacheValue } }, expiration)); - Exception e = null; + bool result; try { - return BaseTrySet(cacheKey, cacheValue, expiration); + result = BaseTrySet(cacheKey, cacheValue, expiration); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteSetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteSetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteSetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteSetCacheAfter(operationId); + return result; } 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; + bool result; try { - return await BaseTrySetAsync(cacheKey, cacheValue, expiration, cancellationToken); + result = await BaseTrySetAsync(cacheKey, cacheValue, expiration, cancellationToken); } catch (Exception ex) { - e = ex; + s_diagnosticListener.WriteSetCacheError(operationId, ex); throw; } - finally - { - if (e != null) - { - s_diagnosticListener.WriteSetCacheError(operationId, e); - } - else - { - s_diagnosticListener.WriteSetCacheAfter(operationId); - } - } + s_diagnosticListener.WriteSetCacheAfter(operationId); + return result; } public TimeSpan GetExpiration(string cacheKey) @@ -947,6 +669,6 @@ protected SearchKeyPattern ProcessSearchKeyPattern(string pattern) protected string HandleSearchKeyPattern(string pattern) { return pattern.Replace("*", string.Empty); - } + } } } diff --git a/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs b/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs index 913ebb10..3c49ee09 100644 --- a/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs +++ b/src/EasyCaching.InMemory/Internal/InMemoryCaching.cs @@ -305,11 +305,7 @@ private static bool FilterByPattern(string key, string searchKey, SearchKeyPatte public IDictionary> GetAll(IEnumerable keys) { - var map = new Dictionary>(); - foreach (string key in keys) - map[key] = Get(key); - - return map; + return keys.ToDictionary(key => key, key => Get(key)); } public IDictionary> GetAll(string prefix = "") @@ -323,13 +319,7 @@ public IDictionary> GetAll(string prefix = "") public int SetAll(IDictionary values, TimeSpan? expiresIn = null) { - if (values == null || values.Count == 0) return 0; - - var list = new List(); - - foreach (var entry in values) list.Add(Set(entry.Key, entry.Value, expiresIn)); - - return list.Count(r => r); + return values?.Count(entry => Set(entry.Key, entry.Value, expiresIn)) ?? 0; } public bool Replace(string key, T value, TimeSpan? expiresIn = null)