private void AssertContentsPerStep<K, V>(LeastRecentlyUsedCache<K, V> sut, Func<K, V> valueCreator, K[] keys, K[][] stepValues) { stepValues.Length.Should().Be(keys.Length, "one result is needed per step key"); for(int i=0;i<keys.Length;++i) { sut.GetOrAdd(keys[i], valueCreator); AssertContents(sut, valueCreator, stepValues[i]); } }
//[Benchmark] public void LRUCache50HitHighContention() { var lru = new LeastRecentlyUsedCache <int, int>(Capacity); var t = Task.Run(() => { for (int i = 0; i < 5000; ++i) { lock (lru) { if ((i % 2) == 0) { lru.GetOrAdd(0, _lru1); } else { lru.GetOrAdd(i, _lru1); } } } }); for (int i = 0; i < 5000; ++i) { lock (lru) { if ((i % 2) == 0) { lru.GetOrAdd(0, _lru1); } else { lru.GetOrAdd(i, _lru1); } } } t.Wait(); }
public object ApexLRUCache() { var lru = new LeastRecentlyUsedCache <int, int>(Capacity); if (HitRate == 0) { for (int i = 0; i < Iterations; ++i) { lru.GetOrAdd(i, _lru1); } return(lru); } if (HitRate == 100) { for (int i = 0; i < Iterations; ++i) { lru.GetOrAdd(0, _lru1); } return(lru); } if (HitRate == 50) { for (int i = 0; i < Iterations; ++i) { if ((i % 2) == 0) { lru.GetOrAdd(0, _lru1); } else { lru.GetOrAdd(i, _lru1); } } return(lru); } if (HitRate == 5) { for (int i = 0; i < Iterations; ++i) { if ((i % 20) == 0) { lru.GetOrAdd(0, _lru1); } else { lru.GetOrAdd(i, _lru1); } } return(lru); } if (HitRate == 95) { for (int i = 0; i < Iterations; ++i) { if ((i % 20) != 0) { lru.GetOrAdd(0, _lru1); } else { lru.GetOrAdd(i, _lru1); } } return(lru); } throw new InvalidOperationException("Unsupported HitRate"); }