views:

64

answers:

1

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 ...

+1  A: 

Hi Julien,

The System.Web.Caching.Cache object is available already for you by adding this to the controller:

this.HttpContext.Cache

That is the already in-built cache that's also available in web forms.

Brian