Problem using the System.Web.Caching.Cache class in ASP.NET

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

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

I can access HttpContext.Current.Cache - but this does not allow me to specify the absolute expiration and expiration in the Insert () method.

Can someone help me?

+7
caching
source share
2 answers

You should be able to have an absolute or sliding expiration by calling an insert on HttpRuntime.Cache. It has several overloads. Example:

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.

+14
source share

I suggest you try the PCache class in the PokeIn library. Even if you are using the FREE edition of this library, there are no restrictions with this class. It has much more functionality than the ASP.NET Cache class, and you do not need to solve these problems. The website has a simple sample project.

0
source share

All Articles