3

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;

2 Answers 2

4

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";
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your reply. But Why I don't have HttpContext.Current.Cache? I have HttpContext.Cache instead...
@user361022 where are you trying to access the Cache from? An aspx page's codebind or a class file?
It's because Cache spans multiple requests. Current refers only to the current request, which has it's own request based Cache (Items).
I am using MVC3. Just trying to do the simplest cache example.
@user361022 That is odd since it should be accessible out of the box with HttpContext.Current.Cache in the System.Web namespace. As longs as you have an active context going (that is what HttpContext.Current is getting) then you should be able to access it.
|
1

You don't have to create an instance. ASP.NET does it for you automatically.

Use the HttpContext.Current.Cache.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.