From 6d5f6ed9f76faa24ab37bfae5db8b22dbb75306b Mon Sep 17 00:00:00 2001 From: Shargon Date: Sun, 4 Aug 2019 14:08:03 +0200 Subject: [PATCH 1/2] Cache changes --- neo.UnitTests/IO/Caching/UT_Cache.cs | 43 ++++++++++++++++++++++++++++ neo/IO/Caching/Cache.cs | 18 ++++-------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/neo.UnitTests/IO/Caching/UT_Cache.cs b/neo.UnitTests/IO/Caching/UT_Cache.cs index 01484ae65e..1c3666965b 100644 --- a/neo.UnitTests/IO/Caching/UT_Cache.cs +++ b/neo.UnitTests/IO/Caching/UT_Cache.cs @@ -25,6 +25,35 @@ public IEnumerator MyGetEnumerator() } } + class CacheDisposableEntry : IDisposable + { + public int Key { get; set; } + public bool IsDisposed { get; private set; } + + public void Dispose() + { + IsDisposed = true; + } + } + + class MyDisposableCache : Cache + { + public MyDisposableCache(int max_capacity) : base(max_capacity) { } + + protected override int GetKeyForItem(CacheDisposableEntry item) + { + return item.Key; + } + + protected override void OnAccess(CacheItem item) { } + + public IEnumerator MyGetEnumerator() + { + IEnumerable enumerable = this; + return enumerable.GetEnumerator(); + } + } + [TestClass] public class UT_Cache { @@ -133,6 +162,20 @@ public void TestRemoveKey() cache.Contains("hello").Should().BeFalse(); } + [TestMethod] + public void TestRemoveDisposableKey() + { + var entry = new CacheDisposableEntry() { Key = 1 }; + var dcache = new MyDisposableCache(100); + dcache.Add(entry); + + entry.IsDisposed.Should().BeFalse(); + dcache.Remove(entry.Key).Should().BeTrue(); + dcache.Remove(entry.Key).Should().BeFalse(); + entry.IsDisposed.Should().BeTrue(); + } + + [TestMethod] public void TestRemoveValue() { diff --git a/neo/IO/Caching/Cache.cs b/neo/IO/Caching/Cache.cs index 45556b8ae3..5b97d3a223 100644 --- a/neo/IO/Caching/Cache.cs +++ b/neo/IO/Caching/Cache.cs @@ -10,15 +10,15 @@ internal abstract class Cache : ICollection, IDisposable { protected class CacheItem { - public TKey Key; - public TValue Value; - public DateTime Time; + public readonly TKey Key; + public readonly TValue Value; + public readonly DateTime Time; public CacheItem(TKey key, TValue value) { this.Key = key; this.Value = value; - this.Time = DateTime.Now; + this.Time = TimeProvider.Current.UtcNow; } } @@ -60,13 +60,7 @@ public int Count } } - public bool IsReadOnly - { - get - { - return false; - } - } + public bool IsReadOnly => false; public Cache(int max_capacity) { @@ -97,7 +91,7 @@ private void AddInternal(TKey key, TValue item) { if (InnerDictionary.Count >= max_capacity) { - //TODO: 对PLINQ查询进行性能测试,以便确定此处使用何种算法更优(并行或串行) + //TODO: Perform a performance test on the PLINQ query to determine which algorithm is better here (parallel or not) foreach (CacheItem item_del in InnerDictionary.Values.AsParallel().OrderBy(p => p.Time).Take(InnerDictionary.Count - max_capacity + 1)) { RemoveInternal(item_del); From 4d2d4400392457a180aa947ea25c25b3f90ed180 Mon Sep 17 00:00:00 2001 From: Shargon Date: Sun, 4 Aug 2019 14:10:14 +0200 Subject: [PATCH 2/2] Update UT_Cache.cs --- neo.UnitTests/IO/Caching/UT_Cache.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neo.UnitTests/IO/Caching/UT_Cache.cs b/neo.UnitTests/IO/Caching/UT_Cache.cs index 1c3666965b..1376863e1a 100644 --- a/neo.UnitTests/IO/Caching/UT_Cache.cs +++ b/neo.UnitTests/IO/Caching/UT_Cache.cs @@ -175,7 +175,6 @@ public void TestRemoveDisposableKey() entry.IsDisposed.Should().BeTrue(); } - [TestMethod] public void TestRemoveValue() { @@ -254,4 +253,4 @@ public void TestDispose() action.ShouldThrow(); } } -} \ No newline at end of file +}