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.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)