Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Using the System.Web.Caching.Cache Class in ASP.NET

So I am working on a project which uses ASP.NET. I am trying to call Cache["key"] but the compiler complains about how System.Web.Caching.Cache is "nat valid at this point".

If I call Cache obj = new Cache(); the obj is always null.

I can access HttpContext.Current.Cache - but this doesnt let me specify an absolute expiration and sliding expiration in the Insert() method.

Can someone help me?

like image 647
Ash Avatar asked Feb 10 '09 03:02

Ash


1 Answers

You should be able to absolute or sliding expiration calling the insert on HttpRuntime.Cache. It has several overloads. Ex:

HttpRuntime.Cache.Insert("EXAMPLE_KEY", 
                        exampleItem, 
                        Nothing,                           
                        DateTime.Now.AddHours(1),
                        System.Web.Caching.Cache.NoSlidingExpiration);

The exact same code should also work with HttpContext.Current.Cache.

like image 116
Jim Petkus Avatar answered Nov 08 '22 21:11

Jim Petkus