zoukankan      html  css  js  c++  java
  • 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面

    Donut Caching是缓存除了部分内容以外的整个页面的最好的方式,在它出现之前,我们使用“输出缓存”来缓存整个页面。

    何时使用Donut Caching

    假设你有一个应用程序中有像“主页”这种页面,它除了用户登录的用户名以外总是给用户呈现相同的或者很少变化的内容。这时你可能需要缓存大部分的内容。加入你使用“输出缓存”并应用一个VaryByParam UserID来做缓存处理,那么整个页面会为每个访问用户生成缓存,但是这当我们有很大量的登陆用户是就不是一个好的缓存方式了。

    为了解决这种问题,Donut Caching提出了为所有用户只缓存一份副本,并且包含一小部分动态的内容,这一小部分动态的内容就像甜甜圈上的洞一样。

    Donut Caching 的Nuget 包

    使用Donut Caching之前,你需要在Visual studio中使用Nuget安装包,一般直接键入命令安装:

    install-package MvcDonutCaching

    image

    安装完成后,你就可以通过在action或者controller上添加DonutOutputCache标签来控制缓存了,大多数OutputCache的标签都可以在DonutOutputCache中使用。

    image

    public class HomeController : Controller
        {
            [DonutOutputCache(Duration = 60,VaryByParam="username")]
            public ActionResult Index(string username)
            {
                return View();
            }
    
            [DonutOutputCache(Duration=60)]
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
        }

    Donut Hole Caching

    Donut Hole Caching和Donut Caching刚好相反,它用来缓存页面中的一小部分。

    何时使用Donut Hole Caching

    假设你有一个应用程序,它需要在每个页面里显示产品列表,那么这时以HTML的形式缓存一个产品列表就是很需要的了,Donut Hole Caching就是设计来处理这种情况的啦,ASP.NET MVC可以通过设置ChildActionOnly标签来支持子请求

    [ChildActionOnly]
            [DonutOutputCache(Duration=60)]
            public ActionResult CategoryList()
            {
                return View(categoryService.GetCategories);
            }
    原文地址:http://www.dotnet-tricks.com/Tutorial/mvc/ODJa210113-Donut-Caching-and-Donut-Hole-Caching-with-Asp.Net-MVC-4.html
  • 相关阅读:
    ADO.NET 根据实体类自动生成添加修改语句仅限Oracle使用
    C# 实体对象作为参数统一去除空格
    jQuery 前端复选框 全选 反选 下拉菜单联动
    C# 后台服务器端 Get 请求函数封装
    服务器404错误页面
    vue 封装公用函数
    Vue 生命周期
    Oracle 查看表结构
    ubuntu源配置
    外观(Facade)模式
  • 原文地址:https://www.cnblogs.com/xiaoyaojian/p/4725496.html
Copyright © 2011-2022 走看看