コード例 #1
0
        public void TestBasicLazyCache()
        {
            // Setting a cell and a cache depending on that cell
            var cell  = new Cell <int>(2);
            var count = 0;
            var cache = new LazyCache <int>(() =>
            {
                count++;
                return(cell.Value * 2);
            });

            Assert.IsFalse(cache.IsCached);
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(0, count);
            Assert.AreEqual(0, cache.BeaconCount);

            // Reading the value for the first time loads the cache
            var value = cache.Value;

            Assert.AreEqual(4, value);
            Assert.IsTrue(cache.IsCached);
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(1, count);
            Assert.AreEqual(1, cache.BeaconCount);

            // Reading the value again doesn't trigger a recalculation
            value = cache.Value;
            Assert.AreEqual(4, value);
            Assert.IsTrue(cache.IsCached);
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(1, count);
            Assert.AreEqual(1, cache.BeaconCount);

            // If we change the cell, the cache reverts back to the initial state
            cell.Value = 3;
            Assert.IsFalse(cache.IsCached);
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(1, count);
            Assert.AreEqual(0, cache.BeaconCount);

            // Reading the value again causes the callback to be rerun
            value = cache.Value;
            Assert.IsTrue(cache.IsCached);
            Assert.AreEqual(2, count);
            Assert.AreEqual(6, value);
            Assert.AreEqual(1, cache.BeaconCount);

            // Setting the cache cuts all dependencies
            cache.Value = 28;
            Assert.IsTrue(cache.IsCached);
            Assert.IsTrue(cache.IsSet);
            Assert.AreEqual(28, cache.Value);
            Assert.AreEqual(0, cache.BeaconCount);

            // This time, no dependencies on cell
            cell.Value++;
            Assert.AreEqual(28, cache.Value);

            // If we clear the cache, we revert back to the original state
            cache.Clear();
            Assert.IsFalse(cache.IsCached);
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(0, cache.BeaconCount);

            // Now we re-read the value again
            value = cache.Value;
            Assert.IsTrue(cache.IsCached);
            Assert.AreEqual(3, count);
            Assert.AreEqual(8, value);
            Assert.AreEqual(1, cache.BeaconCount);
        }
コード例 #2
0
        public void TestCascadingLazyCache()
        {
            // We set one cell and two caches in cascade
            var cell   = new Cell <int>(2);
            var cache1 = new LazyCache <int>(() => cell.Value + 1);
            var cache2 = new LazyCache <int>(() => cache1.Value * 2);

            Assert.IsFalse(cache1.IsCached);
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.IsFalse(cache2.IsCached);
            Assert.AreEqual(0, cache2.BeaconCount);

            // Getting the value of cache number 2
            var value = cache2.Value;

            Assert.IsTrue(cache1.IsCached);
            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.IsTrue(cache2.IsCached);
            Assert.AreEqual(1, cache2.BeaconCount);

            // Checking that setting the base cell invalidate all caches
            cell.Value = 3;
            Assert.IsFalse(cache1.IsCached);
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.IsFalse(cache2.IsCached);
            Assert.AreEqual(0, cache2.BeaconCount);

            // Querying the value of cache1
            value = cache1.Value;
            Assert.IsTrue(cache1.IsCached);
            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.AreEqual(4, value);
            Assert.IsFalse(cache2.IsCached);
            Assert.AreEqual(0, cache2.BeaconCount);

            // Get the value of cache2 now
            value = cache2.Value;
            Assert.IsTrue(cache1.IsCached);
            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.IsTrue(cache2.IsCached);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(8, value);

            // Set cache1 to an arbitrary value. This should invalidate the cache2
            cache1.Value = 34;
            Assert.IsTrue(cache1.IsCached);
            Assert.IsFalse(cache2.IsCached);
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.AreEqual(0, cache2.BeaconCount);

            // Get the value of cache2
            value = cache2.Value;
            Assert.IsTrue(cache1.IsCached);
            Assert.IsTrue(cache2.IsCached);
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(68, value);

            // Setting the cell to a different value should have no impact
            cell.Value++;
            Assert.IsTrue(cache1.IsCached);
            Assert.IsTrue(cache2.IsCached);
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(68, cache2.Value);

            // Clearing the cache1 should invalidate cache2
            cache1.Clear();
            Assert.IsFalse(cache1.IsCached);
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.IsFalse(cache2.IsCached);
            Assert.AreEqual(0, cache2.BeaconCount);

            // The value of cache2 should be different now
            value = cache2.Value;
            Assert.IsTrue(cache1.IsCached);
            Assert.IsTrue(cache2.IsCached);
            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(10, value);
        }