Exemplo n.º 1
0
        public void Put_should_put_item_using_passed_sliding_ttl_zero()
        {
#if PORTABLE
            MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
#else
            MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default;
#endif

            string key   = "anything";
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);

            TimeSpan minExpiration =
#if PORTABLE
                TimeSpan.FromMilliseconds(1) // This is the minimum permitted sliding ttl for .NetStandard
#else
                TimeSpan.Zero
#endif
            ;

            Ttl ttl = new Ttl(minExpiration, false);
            provider.Put(key, value, ttl);

            Thread.Sleep(TimeSpan.FromMilliseconds(10));

#if PORTABLE
            object got;
            memoryCache.TryGetValue(key, out got);
#else
            object got = memoryCache[key];
#endif
            got.Should().BeNull();
        }
        public void Put_should_put_item_using_passed_nonsliding_ttl()
        {
            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            TimeSpan shimTimeSpan = TimeSpan.FromSeconds(0.1); // If test fails transiently in different environments, consider increasing shimTimeSpan.

            string key   = Guid.NewGuid().ToString();
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(shimTimeSpan, false);

            provider.Put(key, value, ttl);

            // Initially (before ttl expires), should be able to get value from cache.
            object got;

            memoryCache.TryGetValue(key, out got);
            got.Should().BeSameAs(value);

            // Wait until the TTL on the cache item should have expired.
            Thread.Sleep(shimTimeSpan + shimTimeSpan);

            memoryCache.TryGetValue(key, out got);

            got.Should().NotBeSameAs(value);
            got.Should().BeNull();
        }
Exemplo n.º 3
0
        public void Put_should_put_item_using_passed_sliding_ttl()
        {
#if PORTABLE
            MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
#else
            MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default;
#endif

            TimeSpan shimTimeSpan = TimeSpan.FromSeconds(1); // If test fails transiently in different environments, consider increasing shimTimeSpan.

            string key   = "anything";
            object value = new object();

            // Place an item in the cache that should last for only 2x shimTimespan
            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(shimTimeSpan + shimTimeSpan, true);
            provider.Put(key, value, ttl);

            // Prove that we can repeatedly get it from the cache over a 5x shimTimespan period, due to repeated access.

            for (int i = 0; i < 5; i++)
            {
#if PORTABLE
                object got;
                memoryCache.TryGetValue(key, out got);
#else
                object got = memoryCache[key];
#endif

                got.Should().BeSameAs(value, $"at iteration {i}");

                Thread.Sleep(shimTimeSpan);
            }
        }
Exemplo n.º 4
0
        public void Put_should_put_item_using_passed_sliding_ttl_maxvalue()
        {
#if PORTABLE
            MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
#else
            MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default;
#endif

            string key   = "anything";
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);

            TimeSpan maxSlidingExpiration =
#if PORTABLE
                TimeSpan.MaxValue
#else
                TimeSpan.FromDays(365) // This is the maximum permitted sliding ttl for .NetFramework4.0 and 4.5 MemoryCache.
#endif
            ;

            Ttl ttl = new Ttl(maxSlidingExpiration, true);
            provider.Put(key, value, ttl);

#if PORTABLE
            object got;
            memoryCache.TryGetValue(key, out got);
#else
            object got = memoryCache[key];
#endif
            got.Should().BeSameAs(value);
        }
        public void Put_should_put_item_using_passed_sliding_ttl()
        {
            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            TimeSpan shimTimeSpan = TimeSpan.FromSeconds(1); // If test fails transiently in different environments, consider increasing shimTimeSpan.

            string key   = Guid.NewGuid().ToString();
            object value = new object();

            // Place an item in the cache that should last for only 2x shimTimespan
            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(shimTimeSpan + shimTimeSpan, true);

            provider.Put(key, value, ttl);

            // Prove that we can repeatedly get it from the cache over a 5x shimTimespan period, due to repeated access.

            for (int i = 0; i < 5; i++)
            {
                object got;
                memoryCache.TryGetValue(key, out got);

                got.Should().BeSameAs(value, $"at iteration {i}");

                Thread.Sleep(shimTimeSpan);
            }
        }
Exemplo n.º 6
0
            public void should_return_value()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo {
                    Num = 13
                }, 5.Seconds());
                var value = cacheProvider.Get <Boo>("key");

                value.Num.Should().
                Be(13);
            }
Exemplo n.º 7
0
            public void should_fail()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo {
                    Num = 13
                }, 5.Seconds());
                cacheProvider.Remove("key");

                cacheProvider.Find <Boo>("key").
                HasValue.Should().
                BeFalse();
            }
Exemplo n.º 8
0
            public void should_fail()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo {
                    Num = 13
                }, 100.Milliseconds());

                Thread.Sleep(500);

                cacheProvider.Find <Boo>("key").
                HasValue.Should().
                BeFalse();
            }
Exemplo n.º 9
0
            public void should_absolutly_expired()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo {
                    Num = 13
                }, 5.Seconds());

                Thread.Sleep(3000);
                cacheProvider.Get <Boo>("key");
                Thread.Sleep(3000);

                Assert.Throws <InvalidOperationException>(() => cacheProvider.Get <Boo>("key"), "Объект должен быть удален из кеша.");
            }
        public void Put_should_put_item_using_passed_nonsliding_ttl_maxvalue()
        {
            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            string key   = "anything";
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(TimeSpan.MaxValue, false);

            provider.Put(key, value, ttl);

            object got;

            memoryCache.TryGetValue(key, out got);
            got.Should().BeSameAs(value);
        }
        public void Put_should_put_item_into_configured_MemoryCacheImplementation()
        {
            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            string key   = Guid.NewGuid().ToString();
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(TimeSpan.FromSeconds(10));

            provider.Put(key, value, ttl);

            object got;

            memoryCache.TryGetValue(key, out got);

            got.Should().BeSameAs(value);
        }
        public void Put_should_put_item_using_passed_sliding_ttl_zero()
        {
            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            string key   = "anything";
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);

            TimeSpan minExpiration = TimeSpan.FromMilliseconds(1);  // This is the minimum permitted sliding ttl for .NetStandard

            Ttl ttl = new Ttl(minExpiration, false);

            provider.Put(key, value, ttl);

            Thread.Sleep(TimeSpan.FromMilliseconds(10));

            object got;

            memoryCache.TryGetValue(key, out got);
            got.Should().BeNull();
        }
Exemplo n.º 13
0
        public void Put_should_put_item_using_passed_nonsliding_ttl()
        {
#if PORTABLE
            MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
#else
            MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default;
#endif

            TimeSpan shimTimeSpan = TimeSpan.FromSeconds(0.1); // If test fails transiently in different environments, consider increasing shimTimeSpan.

            string key   = "anything";
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(shimTimeSpan, false);
            provider.Put(key, value, ttl);

            // Initially (before ttl expires), should be able to get value from cache.
#if PORTABLE
            object got;
            memoryCache.TryGetValue(key, out got);
#else
            object got = memoryCache[key];
#endif
            got.Should().BeSameAs(value);

            // Wait until the TTL on the cache item should have expired.
            Thread.Sleep(shimTimeSpan + shimTimeSpan);

#if PORTABLE
            memoryCache.TryGetValue(key, out got);
#else
            got = memoryCache[key];
#endif

            got.Should().NotBeSameAs(value);
            got.Should().BeNull();
        }
Exemplo n.º 14
0
        public void Put_should_put_item_using_passed_nonsliding_ttl_maxvalue()
        {
#if PORTABLE
            MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
#else
            MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default;
#endif

            string key   = "anything";
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(TimeSpan.MaxValue, false);
            provider.Put(key, value, ttl);

#if PORTABLE
            object got;
            memoryCache.TryGetValue(key, out got);
#else
            object got = memoryCache[key];
#endif
            got.Should().BeSameAs(value);
        }
        public void Put_should_put_item_into_configured_MemoryCacheImplementation()
        {
#if PORTABLE
            MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
#else
            MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default;
#endif

            string key   = Guid.NewGuid().ToString();
            object value = new object();

            MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
            Ttl ttl = new Ttl(TimeSpan.FromSeconds(10));
            provider.Put(key, value, ttl);

#if PORTABLE
            object got;
            memoryCache.TryGetValue(key, out got);
#else
            object got = memoryCache[key];
#endif

            got.Should().BeSameAs(value);
        }
Exemplo n.º 16
0
            public void should_return_value()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo { Num = 13 }, 5.Seconds());
                var value = cacheProvider.Get<Boo>("key");

                value.Num.Should().
                    Be(13);
            }
Exemplo n.º 17
0
            public void should_fail()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo { Num = 13 }, 5.Seconds());
                cacheProvider.Remove("key");

                cacheProvider.Find<Boo>("key").
                    HasValue.Should().
                    BeFalse();
            }
Exemplo n.º 18
0
            public void should_fail()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo { Num = 13 }, 100.Milliseconds());

                Thread.Sleep(500);

                cacheProvider.Find<Boo>("key").
                    HasValue.Should().
                    BeFalse();
            }
Exemplo n.º 19
0
            public void should_absolutly_expired()
            {
                var cacheProvider = new MemoryCacheProvider();

                cacheProvider.Put("key", new Boo { Num = 13 }, 5.Seconds());

                Thread.Sleep(3000);
                cacheProvider.Get<Boo>("key");
                Thread.Sleep(3000);

                Assert.Throws<InvalidOperationException>(() => cacheProvider.Get<Boo>("key"), "Объект должен быть удален из кеша.");
            }