public void DontCacheExceptions() { var i = 0; var cache = new LazyCache <string>((k) => { i++; if (i == 1) { throw new Exception(); } return("test"); }, 1); try { var first = cache.Get("key"); } catch (Exception) { } var second = cache.Get("key"); Assert.IsTrue(second == "test"); }
public void OnlyOneInParallel_LongGetOperation() { var i = 0; var cache = new LazyCache <string>((k) => { i++; Thread.Sleep(1000); return("test"); }, 1); Parallel.ForEach(Enumerable.Range(1, 3), (a) => cache.Get("key")); Assert.IsTrue(i == 1); }
public void OnlyOneInParallel() { var i = 0; var cache = new LazyCache <string>((k) => { i++; return("test"); }, 1); Parallel.ForEach(Enumerable.Range(1, 5), (a) => cache.Get("key")); Assert.IsTrue(i == 1); }
public void CacheIsAutoRefreshing() { var i = 0; var cache = new LazyCache <string>((k) => { i++; if (i == 2) { Thread.Sleep(200); } return("test"); }, 1.0 / 60); var first = cache.Get("key"); Thread.Sleep(1500); var duration = Duration(() => { var second = cache.Get("key"); }); Assert.IsTrue(duration.TotalMilliseconds < 200); }