简体   繁体   中英

What is ASP.NET Micro-Caching?

What is ASP.NET MVC Micro-Caching?

What is the advantages and disadvantages of a One-Second Cache?

What is the best way to implement Micro-Caching in ASP.NET MVC project with C#?

Just add [OutputCache(Duration = 1)] or ... ?

One-second cache can avoid a large number of clients request and executing a database query at the same time.


If don't use cache, when a lot of request post to server at same time, will cause reduced sever performance.

And because cache time doesn't long, page will always get fresh data every seconds.

Short time cache have a advantages of both, it can handle a large request at the same time, besides get fresh data every seconds.

But on the other hand, short time cache can't keep data for long.


And ASP.NET MVC provided a client/server page cache attribute, just adding a attribute on action(or an entire controller) above. It will cache all action(or controller).

Example :

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        [OutputCache(Duration = 3)]
        public ActionResult Index()
        {
            return View();
        }
    }
}

This code will cache this action for 3 sec and OutputCache default Location is Any (cache clinet and server).

cache in client side:

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        [OutputCache(Duration = 3, Location=OutputCacheLocation.Client)]
        public ActionResult Index()
        {
            return View();
        }
    }
}

cache in server side:

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        [OutputCache(Duration = 3, Location=OutputCacheLocation.Server)]
        public ActionResult Index()
        {
            return View();
        }
    }
}

In addition,adding VaryByParam property let cache can vary by parameter. In same action, user use different parameter will get different cache, same parameter get same cache version.

This can use to a cache like product info page.

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        [OutputCache(Duration = 3, VaryByParam = "id")]
        public ActionResult ProductDetail(int id)
        {
            ViewBag.detail = id;
            return View();
        }
    }
}

OutputCache has a many property and feature , you can visit msdn get more info .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM