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); })); }
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; } )); }
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)); }