Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about ASP.NET Cache class

I would like to use Cache for my web application.

Here is the question

I found this link, http://msdn.microsoft.com/en-us/library/18c1wd61.aspx

For that link, all the example use something like Cache["KeyName"]="blah blah blah";

When I try to do the same thing, I have an error message, said "using System.Web.Caching.Cache is a type but used like a variable"

What should I do?

Do I have to create an instance?

My Example

string test = "123";
  if (HttpContext.Cache["test"] != null)
    test = (string)HttpContext.Cache["test"];
else
    HttpContext.Cache["test"] = test;
like image 988
HorseKing Avatar asked Nov 27 '25 10:11

HorseKing


1 Answers

I think you are getting some overlap of naming. Be explicit and see if it works:

HttpContext.Current.Cache["KeyName"]="blah blah blah";

You can also do the following in your ASP.NET codebehind:

Page.Cache["KeyName"]="blah blah blah";

or

this.Cache["KeyName"]="blah blah blah";

Cache is handled by ASP.NET so you just have to use it, not create it.

EDIT: In ASP.NET MVC you can use the following in your controller:

HttpContext.Cache["KeyName"]="blah blah blah";
like image 82
Kelsey Avatar answered Nov 28 '25 23:11

Kelsey