I'm running on VS 2010 Ultimate on Windows 7 and trying to use MemoryCache (System.Runtime.Caching) and for some reason, the cache is immediately cleared when the method ends and when I re-run the method again, it is trying to create a new one. Here is the code that I'm using from MSDN docs:
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
List<string> filePaths = new List<string>();
filePaths.Add("c:\\Windows\\Enterprise.xml");
// policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
policy.Priority = CacheItemPriority.NotRemovable;
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(3600);
// Fetch the file contents.
fileContents =
File.ReadAllText("c:\\Windows\\Enterprise.xml");
cache.Set("filecontents", fileContents, policy);
}
Console.WriteLine(fileContents);
I have this code in the Console Main method.
The surprising thing is I have a wrapper C# 4.0 assembly that I'm consuming from QTP and it is working absolutely good. The cache stays on each run in QTP.
Please help.