public void CacheSizeLimitedTest() { using (var cache = new CxLimitedCache <CxFund>(1, TimeSpan.FromHours(1))) { var fund1 = new CxFund { Id = 1, Key = "test1" }; var fund2 = new CxFund { Id = 2, Key = "test2" }; CxFund temp; cache.AddValue(fund1); Assert.AreEqual(cache.Count, 1); cache.AddValue(fund2); Assert.AreEqual(cache.Count, 1); Assert.IsTrue(cache.TryGetValue(fund2.Id, out temp)); Assert.AreEqual(fund2, temp); Assert.IsTrue(cache.TryGetValue(fund2.Key, out temp)); Assert.AreEqual(fund2, temp); Assert.IsFalse(cache.TryGetValue(fund1.Id, out temp)); Assert.IsNull(temp); Assert.IsFalse(cache.TryGetValue(fund1.Key, out temp)); Assert.IsNull(temp); } }
static void Main(string[] args) { using (var cache = new CxLimitedCache <CxElement>(100000, TimeSpan.FromSeconds(4), 5, null)) { var watch = Stopwatch.StartNew(); for (var i = 0; i < 100000; i++) { cache.AddValue(new CxElement(i, string.Format("key.{0}", i))); } watch.Stop(); Console.WriteLine("insert time {0}, {1}", watch.ElapsedMilliseconds, cache.Count); ////======================================================================================================== watch = Stopwatch.StartNew(); var stopTime = DateTime.Now.AddSeconds(5); var rnd = new Random(); var accessdCount = 0; var findCount = 0; while (DateTime.Now < stopTime) { CxElement value; cache.TryGetValue(rnd.Next(99999), out value); accessdCount++; } watch.Stop(); Console.WriteLine( "access time no expiration {0}, accessed {1} times, timer starts {2}, current size {3}", watch.ElapsedMilliseconds, accessdCount, cache.TimerStartsCount, cache.Count); ////======================================================================================================== cache.TimerInterval = TimeSpan.FromMilliseconds(200); watch = Stopwatch.StartNew(); stopTime = DateTime.Now.AddSeconds(5); accessdCount = 0; findCount = 0; while (DateTime.Now < stopTime) { CxElement value; cache.TryGetValue(rnd.Next(99999), out value); accessdCount++; } watch.Stop(); Console.WriteLine( "access time with expiration {0}, accessed {1} times, timer starts {2}, current size {3}", watch.ElapsedMilliseconds, accessdCount, cache.TimerStartsCount, cache.Count); } }
public void CacheThrowsExceptionOnBadKeysTest() { using (var cache = new CxLimitedCache <CxFund>(100, TimeSpan.FromMinutes(5))) { var fund1 = new CxFund { Id = 1, Key = "test" }; var fund2 = new CxFund { Id = 2, Key = "test" }; cache.AddValue(fund1); cache.AddValue(fund2); } }
public void CacheExpiresByTimeTest() { using (var cache = new CxLimitedCache <CxFund>(100, 100, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2))) { var fund = new CxFund { Id = 1, Key = "test1" }; CxFund temp; cache.AddValue(fund); Assert.AreEqual(cache.Count, 1); Assert.IsTrue(cache.TryGetValue(fund.Id, out temp)); Assert.AreEqual(fund, temp); Thread.Sleep(TimeSpan.FromSeconds(4)); Assert.AreEqual(cache.Count, 0); } }
public void CacheExpiresByAccessCountTest() { using (var cache = new CxLimitedCache <CxFund>(100, 1, TimeSpan.FromSeconds(600), TimeSpan.FromSeconds(60))) { var fund = new CxFund { Id = 1, Key = "test1" }; CxFund temp; cache.AddValue(fund); Assert.AreEqual(cache.Count, 1); Assert.IsTrue(cache.TryGetValue(fund.Id, out temp)); Assert.AreEqual(fund, temp); Assert.AreEqual(cache.Count, 0); Assert.IsFalse(cache.TryGetValue(fund.Id, out temp)); Assert.IsNull(temp); } }