Ejemplo n.º 1
0
        public bool Add(string key, object value, DateTimeOffset absExpiration)
        {
            Delete(key);

            var newCacheEntry = _memoryCache.CreateEntry(key);

            newCacheEntry.Value = value;
            newCacheEntry.AbsoluteExpiration = absExpiration;

            return(_memoryCache.TryGetValue(key, out _));
        }
Ejemplo n.º 2
0
        public void NegativeSizeOnMemoryCacheEntryThrows()
        {
            var cache = new MemoryCache(new MemoryCacheOptions());

            using (var cacheEntry = cache.CreateEntry(new object()))
            {
                Assert.Throws <ArgumentOutOfRangeException>(() => { cacheEntry.Size = -1; });
                Assert.Throws <ArgumentOutOfRangeException>(() => { cacheEntry.SetSize(-1); });
            }
        }
Ejemplo n.º 3
0
        public void TestMemoryObject2()
        {
            var cache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions());

            var entry = cache.CreateEntry("user");

            entry.Value = new User {
                Name = "Foo"
            };


            cache.TryGetValue("user", out User f);
            Assert.Equal("Foo", f.Name);

            f.Name = "Bar";
            cache.TryGetValue("user", out User b);
            Assert.Equal("Bar", b.Name);
        }
Ejemplo n.º 4
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);
            }
        }