Esempio n. 1
0
 public static async Task <string> GetTestDataWithCachingAsync(string key, CacheItemPolicy overrideCacheItemPolicy = null)
 {
     return(await TestCacheFacade.GetCachedDataAsync(new TestCacheParams(key, overrideCacheItemPolicy), async() =>
     {
         return await SomeLongRunningMethodAsync(DateTime.Now);
     }));
 }
Esempio n. 2
0
 public static string GetTestDataWithCaching(string key, CacheItemPolicy overrideCacheItemPolicy = null)
 {
     return(TestCacheFacade.GetCachedData(new TestCacheParams(key, overrideCacheItemPolicy), () =>
     {
         return SomeLongRunningMethod(DateTime.Now);
     }));
 }
Esempio n. 3
0
 public static string GetTestDataWithCachingAndTTL(string key, int secondsTTL)
 {
     return(TestCacheFacade.GetCachedData(
                new TestCacheParams(key, secondsTTL),
                () =>
     {
         return SomeLongRunningMethod(DateTime.Now);
     }
                ));
 }
Esempio n. 4
0
 public static async Task <string> GetTestDataWithCachingAndTTLAsync(string key, int secondsTTL)
 {
     return(await TestCacheFacade.GetCachedDataAsync(
                new TestCacheParams(key, secondsTTL),
                async() =>
     {
         string result = await SomeLongRunningMethodAsync(DateTime.Now);
         return result;
     }
                ));
 }
Esempio n. 5
0
        public async Task TestCacheThreadSafetyWithLazyInitializationAsync()
        {
            string key         = $"CachedDataWithSameKey[{nameof(TestCacheThreadSafetyWithLazyInitializationAsync)}]";
            int    secondsTTL  = 300;
            int    threadCount = 20;
            var    globalCount = 0;
            var    timer       = Stopwatch.StartNew();

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

            for (int x = 0; x < threadCount; x++)
            {
                //Simulated MANY threads running at the same time attempting to get the same data for the same Cache key!!!
                tasks.Add(Task.Run(async() =>
                {
                    //THIS RUNS ON IT'S OWN THREAD, but the Lazy Cache initialization will ensure that the Value Factory Function
                    //  is only executed by the FIRST thread, and all other threads will immediately benefit from the result!
                    return(await TestCacheFacade.GetCachedDataAsync(new TestCacheParams(key, secondsTTL), async() =>
                    {
                        //TEST that this Code ONLY ever runs ONE TIME by ONE THREAD via Lazy<> initialization!
                        //  meaning globalCount is only ever incremented 1 time!
                        Interlocked.Increment(ref globalCount);

                        //TEST that the cached data is never re-generated so only ONE Value is ever created!
                        var longTaskResult = await SomeLongRunningMethodAsync(DateTime.Now);
                        return longTaskResult;
                    }));
                }));
            }

            //Allow all threads to complete and get the results...
            var results = await Task.WhenAll(tasks.ToArray());

            var distinctCount = results.Distinct().Count();

            timer.Stop();

            //TEST that this Code ONLY ever runs ONE TIME by ONE THREAD via Lazy<> initialization!
            //  meaning globalCount is only ever incremented 1 time!
            Assert.AreEqual(1, globalCount);

            //Ensure ONLY ONE item was ever generated and ALL other's were identical from Cache!
            Assert.AreEqual(1, distinctCount);

            //Ensure that the Total time takes barely longer than one iteration of the Long Running Task!
            Assert.IsTrue(timer.ElapsedMilliseconds < (LongRunningTaskMillis * 2));
        }
Esempio n. 6
0
        public void TestCacheRemoval()
        {
            string key     = $"CachedDataWithSameKey[{nameof(TestCacheRemoval)}]";
            var    result1 = GetTestDataWithCaching(key);
            var    result2 = GetTestDataWithCaching(key);

            Assert.AreEqual(result1, result2);
            Assert.AreSame(result1, result2);

            TestCacheFacade.RemoveCachedData(new TestCacheParams(key));

            //Since it's removed then it MUST be newly Initialized
            //  resulting in a Different Result being returned from Result1
            var result3 = GetTestDataWithCaching(key);

            Assert.AreNotEqual(result1, result3);
            Assert.AreNotSame(result1, result3);
        }