Implement System.Web.Caching.Cache Object in a controller to cache specific query
Posted
by Julien
on Stack Overflow
See other posts from Stack Overflow
or by Julien
Published on 2010-03-29T15:07:38Z
Indexed on
2010/03/29
15:23 UTC
Read the original article
Hit count: 514
Hello World!
I'm Julien and I have a question is it correct to implement my caching object like this in my controller :
public class HomeController : BaseController
{
public static Cache cachingControl = new Cache();
...
And I Use it like this :
public ActionResult Home()
{
IndexViewData view = new IndexViewData();
view.UserId = UserId;
if (cachingControl.Get("viewHome") != null) {
view = (IndexViewData)cachingControl.Get("viewHome");
} else {
view.allAdsList = AllAds(5000, 0);
if (Request.QueryString["voirTous"] != null)
view.loadGeneral(true);
else
view.loadGeneral(false);
cachingControl.Insert("viewHome", view);
}
view.adsList = SetupSearch(5, false, 0);
return View(view);
}
But When I Call this line :
if (cachingControl.Get("viewHome") != null) {
They trow me the error
NullErrorException
But I know it can be null this is why i'm put this condition to
Do you have an alternative or a tips for me thank you!
P.S.: I Know that the code is weird :P but I must to support it ...
Julien
© Stack Overflow or respective owner