开发者

Implement System.Web.Caching.Cache Object in a controller to cache specific query

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("viewHo开发者_开发问答me") != 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 ...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜