1. create helper class
public static class CacheHelper
{
static readonly ObjectCache Cache = MemoryCache.Default;
public static void SaveTocache(string cacheKey, object savedItem, DateTime absoluteExpiration)
{
MemoryCache.Default.Set(cacheKey, savedItem, absoluteExpiration);
}
public static T GetFromCache<T>(string cacheKey) where T : class
{
return MemoryCache.Default[cacheKey] as T;
}
public static void RemoveFromCache(string cacheKey)
{
MemoryCache.Default.Remove(cacheKey);
}
public static bool IsIncache(string cacheKey)
{
return MemoryCache.Default[cacheKey] != null;
}
public static T Get<T>(string cacheKey, string regionName = null) where T : class
{
return MemoryCache.Default.Get(cacheKey) as T;
}
/// <summary>
/// Gets all cached items as a list by their key.
/// </summary>
/// <returns></returns>
public static List<string> GetAll()
{
return Cache.Select(keyValuePair => keyValuePair.Key).ToList();
}
}
2. in base class add
/// <summary>
/// The general test clean.
/// </summary>
[TestCleanup]
public void GeneralTestClean()
{
this.TestContextWithTimer.EndTimer(TestName);
//Create a custom Timeout of 10 seconds
DateTime customTimeout = DateTime.Now.AddSeconds(60.0);
testDetailed.TestName = TestName;
testDetailed.Date = DateTime.Now;
testDetailed.Duration = this.TestContextWithTimer.TestDuration(testName).ToString();
testDetailed.PassFail = (Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome.Passed == this.TestContext.CurrentTestOutcome ? "Pass" : "Fail");
result.Add(testDetailed);
CacheHelper.SaveTocache(CacheKeyValue, result, customTimeout);
//get list of keys
List<string> getCacheKeyList = CacheHelper.GetAll();
getCacheValueList = new Dictionary<string, List<string>>();
foreach (var item in getCacheKeyList)
{
var getCacheValueFromCache = CacheHelper.GetFromCache<List<string>>(item);
getCacheValueList.Add(item, getCacheValueFromCache);
}
this.client.Dispose();
}
private readonly string CacheKeyValue = "TestResultDetails " + DateTime.Now;
private static Dictionary<string, List<string>> getCacheValueList;
private static List<TestDetailed> endResult = new List<TestDetailed>();
[AssemblyCleanup]
public static void ClassCleanup()
{
foreach (string key in getCacheValueList.Keys)
{
foreach (string val in getCacheValueList[key])
{
Console.WriteLine(val);
}
}
}
Немає коментарів:
Дописати коментар