1

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.

4
  • 1
    so if this is in the Main method, does the program close after this or does it enter into a loop ? Commented Jun 19, 2013 at 23:37
  • The control exits the method and the debugger stops. But then, when I run the method again in debug mode, it sees that the cache is empty and goes into the if clause. Visual Studio IDE is still open. I'm assuming the cache should stay as I have a expiration set to 1 hour and is not removable. Commented Jun 19, 2013 at 23:39
  • I think the problem is that whilst you've set it to be an hour and not removable; you're doing so from a Static method which means the context for the cache disappears when the debugger detaches and it gets garbage collected. I'm just theory-crafting though ... Commented Jun 19, 2013 at 23:45
  • Can you please try the same code from the console main method and see if you can duplicate the issue? Commented Jun 19, 2013 at 23:51

1 Answer 1

1

The lifetime of the MemoryCache is not tied at all to Visual Studio (you mentioned in a comment that "Visual Studio IDE is still open."

The cache is available only while your program is running, so each time you stop and restart your application the cache will in fact be empty. More specifically, it resides within the context of an AppDomain, which is created when you start your application and destroyed when your application exits.

Only if your application runs for more than an hour would that policy have any effect.

Sign up to request clarification or add additional context in comments.

4 Comments

That's surprising and also confusing. When I instantiate the same MemoryCache from QTP automation tool and add something to the cache with a sliding expiration of 120 minutes, it is available when I run my method and even after the method finishes. When I re-run the method from QTP, I'm able to see it again. So, there is something that is not correct when I do the same from Visual Studio.
@user766237 If QTP is a web-based tool, then the cache lives while the web host is running. When you start debugging through Visual Studio, a "lite" web server (Cassini) spins up as another process and that hosts the server side of your project. With that running, you can debug the front-end over and over against the same running instance of the server component. So, the cache is alive in that server component until you shut it down, even though you are starting and stopping the front-end.
No. QTP (Quick Test Pro) is a desktop application running on Windows 7. It is the same as Visual Studio.
not sure how QTP works. may be its runtime is still loaded in ram and that is why the cache is still available when you access it later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.