コード例 #1
0
        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");
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }