Skip to content

Commit

Permalink
Small cache changes (neo-project#994)
Browse files Browse the repository at this point in the history
* Cache changes

* Update UT_Cache.cs
  • Loading branch information
shargon authored and Tommo-L committed Jun 22, 2020
1 parent c13ad46 commit 1a1dbaf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
44 changes: 43 additions & 1 deletion neo.UnitTests/IO/Caching/UT_Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, CacheDisposableEntry>
{
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
{
Expand Down Expand Up @@ -133,6 +162,19 @@ 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()
{
Expand Down Expand Up @@ -211,4 +253,4 @@ public void TestDispose()
action.ShouldThrow<ObjectDisposedException>();
}
}
}
}
18 changes: 6 additions & 12 deletions neo/IO/Caching/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ internal abstract class Cache<TKey, TValue> : ICollection<TValue>, 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;
}
}

Expand Down Expand Up @@ -60,13 +60,7 @@ public int Count
}
}

public bool IsReadOnly
{
get
{
return false;
}
}
public bool IsReadOnly => false;

public Cache(int max_capacity)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1a1dbaf

Please sign in to comment.