Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small cache changes #994

Merged
merged 3 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
shargon marked this conversation as resolved.
Show resolved Hide resolved
}
}

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikzhang, how about this TODO?
Is it still an open question? We may investigate this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is.

foreach (CacheItem item_del in InnerDictionary.Values.AsParallel().OrderBy(p => p.Time).Take(InnerDictionary.Count - max_capacity + 1))
{
RemoveInternal(item_del);
Expand Down