2

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

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.