public void Size_3_Cache_Add_4_Get_Value_1_Should_Return_False()
        {
            MemoryCache <Key, Value> cache = new MemoryCache <Key, Value>(3, new KeyComparer());

            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 1, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 2, y = 1
            }, new Value {
                a = 2, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 3, y = 1
            }, new Value {
                a = 3, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 4, y = 1
            }, new Value {
                a = 4, b = 1
            });
            Value value;

            Assert.IsFalse(cache.TryGetValue(new Key {
                x = 1, y = 1
            }, out value));
        }
        public void Size_3_Cache_Add_3_Update_1_To_11_Get_Value_1_Should_Return_11()
        {
            MemoryCache <Key, Value> cache = new MemoryCache <Key, Value>(3, new KeyComparer());

            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 1, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 2, y = 1
            }, new Value {
                a = 2, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 3, y = 1
            }, new Value {
                a = 3, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 11, b = 1
            });
            Value value;

            Assert.IsTrue(cache.TryGetValue(new Key {
                x = 1, y = 1
            }, out value));
            Assert.AreEqual(11, value.a);
        }
        public void Size_2_Cache_Add_1_Update_Value_Cache_Size_Should_Be_1()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(2);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(1, 2);
            Assert.AreEqual(1, cache.CurrentSize);
        }
Exemplo n.º 4
0
        public void MustGiveSizeOption()
        {
            var cache   = new MemoryCache <int, string>(10);
            var options = new CacheEntryOptions();

            Assert.ThrowsException <ArgumentException>(() => cache.AddOrUpdate(10, "hello"));
            Assert.ThrowsException <ArgumentException>(() => cache.AddOrUpdate(10, "hello", options));
        }
        public void Size_1_Cache_Add_2_Get_Value_1_Should_Return_False()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(1);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);
            int value;

            Assert.IsFalse(cache.TryGetValue(1, out value));
        }
        public void Size_1_Cache_Add_1_Update_Value_To_5_Value_Should_Be_5()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(1);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(1, 5);
            int value;

            cache.TryGetValue(1, out value);
            Assert.AreEqual(5, value);
        }
        public void Size_1_Cache_Add_2_Get_Value_2_Should_Return_2()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(1);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);
            int value;

            Assert.IsTrue(cache.TryGetValue(2, out value));
            Assert.AreEqual(2, value);
        }
        public void Size_3_Cache_Add_4_Get_Value_4_Should_Return_True()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(3);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);
            cache.AddOrUpdate(3, 3);
            cache.AddOrUpdate(4, 4);
            int value;

            Assert.IsTrue(cache.TryGetValue(4, out value));
        }
        public void Size_3_Cache_Add_2_Update_1_To_11_Get_Value_1_Should_Return_11()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(3);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);
            cache.AddOrUpdate(1, 11);
            int value;

            Assert.IsTrue(cache.TryGetValue(1, out value));
            Assert.AreEqual(11, value);
        }
        public void Size_3_Cache_Add_4_Update_3_To_33_Get_Value_3_Should_Return_33()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(3);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);
            cache.AddOrUpdate(3, 3);
            cache.AddOrUpdate(4, 4);
            cache.AddOrUpdate(3, 33);
            int value;

            Assert.IsTrue(cache.TryGetValue(3, out value));
            Assert.AreEqual(33, value);
        }
Exemplo n.º 11
0
        public void CanReplaceADuplicate()
        {
            var cache = new MemoryCache <string, string>();

            cache.AddOrUpdate("abc", "def");
            cache.AddOrUpdate("abc", "deef");

            Assert.AreEqual(1, cache.Count);
            Assert.AreEqual(0, cache.Size);

            var result = cache.TryGetValue("abc", out var value);

            Assert.IsTrue(result);
            Assert.AreEqual("deef", value);
        }
Exemplo n.º 12
0
        public void CanAddAnEntry()
        {
            var cache = new MemoryCache <string, string>();

            cache.AddOrUpdate("abc", "def");
            Assert.AreEqual(1, cache.Count);
        }
        public void Size_2_Cache_Add_2_Cache_Size_Should_Be_2()
        {
            MemoryCache <Key, Value> cache = new MemoryCache <Key, Value>(2, new KeyComparer());

            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 1, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 2, y = 1
            }, new Value {
                a = 2, b = 2
            });
            Assert.AreEqual(2, cache.CurrentSize);
        }
        public void Cache_AddOrUpdate_SameKey_ShouldRenewLifetime()
        {
            int capacity      = 5;
            int itemsToInsert = 5;
            int keyStart      = 0;
            int keyOfInterest = 1; // the specific key in the cache to test against

            var memcache = new MemoryCache <int, SimpleTestClass>(capacity);

            // Insert as many entries as needed
            ParallelAddOrUpdateHelper <int, SimpleTestClass>(itemsToInsert, memcache, () => { return(keyStart++); }, () => { return(new SimpleTestClass()); });

            // Assert that the first key added is _not_ the youngest item
            // This is a poor test as who knows what order ParallelHelper inserted them.
            var initalKeyAge = memcache.KeysByAge;

            Assert.AreNotEqual(capacity - 1, Array.IndexOf(initalKeyAge, keyOfInterest));

            // Update keyOfInterest to trigger a renew on lifetime
            memcache.AddOrUpdate(keyOfInterest, new SimpleTestClass());

            // Assert that the keyOfInterest is the youngest now
            var updatedKeyAge = memcache.KeysByAge;

            Assert.IsTrue((capacity - 2 <= Array.IndexOf(updatedKeyAge, keyOfInterest)));
        }
        public void Size_0_Cache_Add_1_Cache_Size_Should_Be_0()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(0);

            cache.AddOrUpdate(1, 1);
            Assert.AreEqual(0, cache.CurrentSize);
        }
Exemplo n.º 16
0
        public void CacheKeepsMostRecentlyUsedItems()
        {
            var cache = new MemoryCache <int, string>(10);

            for (int i = 0; i < 15; i++)
            {
                cache.AddOrUpdate(i, RandomUtils.GetInt32().ToString());

                if (i == 8)
                {
                    // Use first 3 items.
                    for (int k = 0; k < 3; ++k)
                    {
                        cache.TryGetValue(k, out string unused);
                    }
                }
            }

            // Cache should have 0-2 & 8-14.
            for (int i = 0; i < 15; i++)
            {
                bool success = cache.TryGetValue(i, out string unused);

                if (i < 3 || i > 7)
                {
                    Assert.True(success);
                }
                else
                {
                    Assert.False(success);
                }
            }

            Assert.Equal(10, cache.Count);
        }
        /// <summary>
        /// This method was to used to track down an issue using Parallel.For in a test context.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="timesToRun"></param>
        /// <param name="cache"></param>
        /// <param name="keyFunction"></param>
        /// <param name="objectToInsertFunction"></param>
        /// <param name="postAction"></param>
        private void DEBUG_ParallelAddOrUpdateHelperWithPostAction <TKey, TValue>(int timesToRun,
                                                                                  MemoryCache <TKey, TValue> cache,
                                                                                  Func <TKey> keyFunction,
                                                                                  Func <TValue> objectToInsertFunction,
                                                                                  Action <int> postAction
                                                                                  )
        {
            // using Parallel.For "amusingly" enough caused the cache to occasionally be in a bad state when asserting in some cases,
            // discovered when trying to Assert that a cache miss _had_ happened, but the thread executing it wasn't reflective
            // of the actual state of the cache.
            // Technically the below code should be the same as the above, however there can be a state where the test will
            // reach the stage to attempt a cache miss, and get in at a moment where the Parallel.For hasn't finished but released a lock,
            // resulting in the AddOrUpdate's still running, while the test is completed.
            // Using Task.WaitAll ensures that everything _is_ finished.

            /*
             * Parallel.For(0, timesToRun, i => {
             *    cache.AddOrUpdate(keyFunction(), objectToInsertFunction());
             *    postAction(i);
             * });*/

            var tasks = new List <Task>(timesToRun);

            for (int i = 0; i < timesToRun; i++)
            {
                cache.AddOrUpdate(keyFunction(), objectToInsertFunction());
                postAction(i); // Run an action, passing the current iteration to it
            }
            Task.WaitAll(tasks.ToArray());
        }
        public void CanAddAndUpdate()
        {
            var cache  = new MemoryCache <string, string>();
            var values = new List <Common.Caching.Abstract.KeyValuePair <string, string> >();

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

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

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

                values.Add(kvp);
            }

            cache.AddOrUpdate(values);
            Assert.AreEqual("value::10", cache["key::10"]);
            Assert.AreEqual("value::50", cache["key::50"]);
            Assert.AreEqual("value::55", cache["key::55"]);
        }
        public void Cache_TryGetValue_SameKey_ShouldRenewLifetime()
        {
            int capacity      = 5;
            int itemsToInsert = 5;
            int keyStart      = 0;
            int keyOfInterest = 1; // the specific key in the cache to test against

            var memcache = new MemoryCache <int, SimpleTestClass>(capacity);

            // Insert as many entries as needed
            ParallelAddOrUpdateHelper <int, SimpleTestClass>(itemsToInsert, memcache, () => { return(keyStart++); }, () => { return(new SimpleTestClass()); });

            // Assert that the first key added is _not_ the youngest item
            // This is a poor test as who knows what order ParallelHelper inserted them.
            var initalKeyAge = memcache.KeysByAge;

            Assert.AreNotEqual(capacity - 1, Array.IndexOf(initalKeyAge, keyOfInterest));

            // Update the keyOfInterest with a value we control
            var newValue = new SimpleTestClass();

            memcache.AddOrUpdate(keyOfInterest, newValue);

            // Get keyOfInterest to renew its lifetime
            memcache.TryGetValue(keyOfInterest, out SimpleTestClass cachedValue);

            // Assert that getting the value updated its lifetime, and that the value did not change
            var updatedKeyAge = memcache.KeysByAge;

            Assert.AreEqual(capacity - 1, Array.IndexOf(updatedKeyAge, keyOfInterest));
            Assert.AreEqual(newValue, cachedValue);
        }
Exemplo n.º 20
0
        public void CanAddNullValue()
        {
            var cache = new MemoryCache <string, string>();

            cache.AddOrUpdate("abc", null);
            Assert.AreEqual(1, cache.Count);
            Assert.AreEqual(0, cache.Size);
        }
        /// <summary>
        /// Wrapper for Parallel.For
        /// <para>
        /// Calls AddOrUpdate on the given cache as many times as timesToRun, using the specified key and value generation functions.
        /// </para>
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="timesToRun"></param>
        /// <param name="cache"></param>
        /// <param name="keyFunction"></param>
        /// <param name="objectToInsertFunction"></param>
        private void ParallelAddOrUpdateHelper <TKey, TValue>(int timesToRun, MemoryCache <TKey, TValue> cache, Func <TKey> keyFunction, Func <TValue> objectToInsertFunction)
        {
            var tasks = new List <Task>(timesToRun);

            for (int i = 0; i < timesToRun; i++)
            {
                cache.AddOrUpdate(keyFunction(), objectToInsertFunction());
            }
            Task.WaitAll(tasks.ToArray());
        }
Exemplo n.º 22
0
        public DatabasePoolController()
        {
            cacheManager = new MemoryCache();
            logger       = new ApplicationInsightsLogger();

            if (!cacheManager.Exist(DatabaseType.Used.GetEnumDescription()).Result)
            {
                cacheManager.AddOrUpdate(new List <string>(), DatabaseType.Used.GetEnumDescription());
            }
            if (!cacheManager.Exist(DatabaseType.Open.GetEnumDescription()).Result)
            {
                cacheManager.AddOrUpdate(new List <string>(), DatabaseType.Open.GetEnumDescription());
            }
            if (!cacheManager.Exist(DatabaseType.BeingCreated.GetEnumDescription()).Result)
            {
                cacheManager.AddOrUpdate(new List <string>(), DatabaseType.BeingCreated.GetEnumDescription());
            }

            dbOperations = new AzureDbOperations();
        }
Exemplo n.º 23
0
        public void WillThrowOnOverflow()
        {
            var cache = new MemoryCache <string, string>(10);
            var opts  = new CacheEntryOptions {
                Size = 15
            };

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => cache.AddOrUpdate("abc", "def", opts));
            Assert.AreEqual(0, cache.Count);
            Assert.AreEqual(0, cache.Size);
        }
Exemplo n.º 24
0
        public void CanUpdateSize()
        {
            var cache = new MemoryCache <string, string>(100);
            var opts  = new CacheEntryOptions {
                Size = 10
            };

            cache.AddOrUpdate("abc", "def", opts);
            Assert.AreEqual(1, cache.Count);
            Assert.AreEqual(10, cache.Size);
        }
        public void Size_1_Cache_Add_1_Update_Value_To_5_Value_Should_Be_5()
        {
            MemoryCache <Key, Value> cache = new MemoryCache <Key, Value>(1, new KeyComparer());

            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 1, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 5, b = 1
            });
            Value value;

            cache.TryGetValue(new Key {
                x = 1, y = 1
            }, out value);
            Assert.AreEqual(5, value.a);
        }
        public void Size_1_Cache_Add_2_Get_Value_2_Should_Return_2()
        {
            MemoryCache <Key, Value> cache = new MemoryCache <Key, Value>(1, new KeyComparer());

            cache.AddOrUpdate(new Key {
                x = 1, y = 1
            }, new Value {
                a = 1, b = 1
            });
            cache.AddOrUpdate(new Key {
                x = 2, y = 1
            }, new Value {
                a = 2, b = 1
            });
            Value value;

            Assert.IsTrue(cache.TryGetValue(new Key {
                x = 2, y = 1
            }, out value));
            Assert.AreEqual(2, value.a);
        }
Exemplo n.º 27
0
        public void CacheDoesNotExceedMaxItemsLimit()
        {
            int maxItemsCount = 100;

            var cache = new MemoryCache <int, string>(maxItemsCount);

            for (int i = 0; i < maxItemsCount * 2; i++)
            {
                cache.AddOrUpdate(i, RandomUtils.GetInt32().ToString());
            }

            Assert.Equal(maxItemsCount, cache.Count);
        }
        public void Cache_AddOrUpdateOnce_And_TryGet_SimpleData()
        {
            int capacity = 1;

            var testValue = new SimpleTestClass();

            var memcache = new MemoryCache <int, SimpleTestClass>(capacity);

            memcache.AddOrUpdate(1, testValue);

            memcache.TryGetValue(1, out SimpleTestClass insertedValue);

            Assert.AreEqual(testValue, insertedValue);
        }
        public void Size_3_Cache_Add_4_Update_3_To_33_Update_1_To_11_Add_5th_Get_Value_3_Add_6th_Get_5_Get_3_Update_1_To_11_Get_6_Should_Return_False()
        {
            MemoryCache <int, int> cache = new MemoryCache <int, int>(3);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);
            cache.AddOrUpdate(3, 3);
            cache.AddOrUpdate(4, 4);
            cache.AddOrUpdate(3, 33);
            cache.AddOrUpdate(1, 11);
            cache.AddOrUpdate(5, 5);
            int value;

            cache.TryGetValue(3, out value);
            cache.AddOrUpdate(6, 6);
            cache.TryGetValue(5, out value);
            cache.TryGetValue(3, out value);
            cache.AddOrUpdate(1, 11);
            Assert.IsFalse(cache.TryGetValue(6, out value));
        }
        public void CacheDoesNotExceedCapacity()
        {
            int capacity      = 10;
            int itemsToInsert = 100;

            var memcache = new MemoryCache <int, SimpleTestClass>(capacity);

            Parallel.For(0, itemsToInsert, i => {
                var newEntry = new SimpleTestClass();

                memcache.AddOrUpdate(i, newEntry);
            });

            Assert.AreEqual(10, memcache.CacheSize);
        }