示例#1
0
        public void GetCurrentStatistics_TrackStatisticsFalse_ReturnsNull()
        {
            var cache = new MemoryCache(new MemoryCacheOptions {
                TrackStatistics = false
            });

            Assert.Null(cache.GetCurrentStatistics());
        }
示例#2
0
        public void GetCurrentStatistics_UpdateAfterExistingItemExpired_CurrentEstimatedSizeResets(bool sizeLimitIsSet)
        {
            const string Key = "myKey";

            var cache = new MemoryCache(sizeLimitIsSet ?
                                        new MemoryCacheOptions {
                TrackStatistics = true, Clock = new SystemClock(), SizeLimit = 10
            } :
                                        new MemoryCacheOptions {
                TrackStatistics = true, Clock = new SystemClock()
            }
                                        );

            ICacheEntry entry;

            using (entry = cache.CreateEntry(Key))
            {
                var expirationToken = new TestExpirationToken()
                {
                    ActiveChangeCallbacks = true
                };
                var mc = new MemoryCacheEntryOptions {
                    Size = 5
                };
                cache.Set(Key, new object(), mc.AddExpirationToken(expirationToken));
                MemoryCacheStatistics?stats = cache.GetCurrentStatistics();

                Assert.NotNull(stats);
                Assert.Equal(1, cache.Count);
                Assert.Equal(1, stats.CurrentEntryCount);
                VerifyCurrentEstimatedSize(5, sizeLimitIsSet, stats);

                expirationToken.HasChanged = true;
                cache.Set(Key, new object(), mc.AddExpirationToken(expirationToken));
                stats = cache.GetCurrentStatistics();

                Assert.NotNull(stats);
                Assert.Equal(0, cache.Count);
                Assert.Equal(0, stats.CurrentEntryCount);
                VerifyCurrentEstimatedSize(0, sizeLimitIsSet, stats);
            }
        }