예제 #1
0
        public void CanAddAndUpdate()
        {
            var cache  = new MemoryCache <string, string>();
            var values = new List <Caching.Abstract.KeyValuePair <string, string> >();

            cache["key::10"] = "abc";
            cache["key::50"] = "def";
            cache["key::55"] = "duf";

            Assert.Equal("abc", cache["key::10"]);
            Assert.Equal("def", cache["key::50"]);
            Assert.Equal("duf", cache["key::55"]);

            for (var idx = 0; idx < 100; idx++)
            {
                var kvp = new Caching.Abstract.KeyValuePair <string, string> {
                    Key   = $"key::{idx}",
                    Value = $"value::{idx}"
                };

                values.Add(kvp);
            }

            cache.AddOrUpdate(values);
            Assert.Equal("value::10", cache["key::10"]);
            Assert.Equal("value::50", cache["key::50"]);
            Assert.Equal("value::55", cache["key::55"]);
        }
예제 #2
0
        public void CannotMassUpdate()
        {
            var cache  = new MemoryCache <string, string>();
            var values = new List <Caching.Abstract.KeyValuePair <string, string> >();

            cache["key::10"] = "abc";
            cache["key::50"] = "def";
            cache["key::55"] = "duf";

            Assert.Equal("abc", cache["key::10"]);
            Assert.Equal("def", cache["key::50"]);
            Assert.Equal("duf", cache["key::55"]);

            for (var idx = 0; idx < 100; idx++)
            {
                var kvp = new Caching.Abstract.KeyValuePair <string, string> {
                    Key   = $"key::{idx}",
                    Value = $"value::{idx}"
                };

                values.Add(kvp);
            }

            Assert.Throws <ArgumentOutOfRangeException>(() => cache.Add(values));
        }
예제 #3
0
        public void CanAddMultipleKVPairs()
        {
            var cache  = new MemoryCache <string, string>();
            var values = new List <Caching.Abstract.KeyValuePair <string, string> >();

            for (var idx = 0; idx < 100; idx++)
            {
                var kvp = new Caching.Abstract.KeyValuePair <string, string> {
                    Key   = $"key::{idx}",
                    Value = $"value::{idx}"
                };

                values.Add(kvp);
            }

            cache.AddOrUpdate(values);
            Assert.Equal("value::50", cache["key::50"]);
        }
예제 #4
0
        public void CanMassTimeout()
        {
            var cache   = new MemoryCache <string, string>();
            var values  = new List <Caching.Abstract.KeyValuePair <string, string> >();
            var options = new CacheEntryOptions {
                Timeout = TimeSpan.FromMilliseconds(50)
            };

            for (var idx = 0; idx < 100; idx++)
            {
                var kvp = new Caching.Abstract.KeyValuePair <string, string> {
                    Key   = $"key::{idx}",
                    Value = $"value::{idx}"
                };

                values.Add(kvp);
            }

            cache.AddOrUpdate(values, options);
            Assert.Equal("value::50", cache["key::50"]);
            Thread.Sleep(51);
            Assert.Throws <ArgumentOutOfRangeException>(() => cache["key::50"]);
        }