使用Asp.Net MVC进行甜甜圈缓存

Donut caching used to cache an entire web page except for one or more small portion of the page. In other words, suppose we want to cache a maximum portion of the view except the minimum portion then we can finalize to use DonutCache. I know while start reading this, many questions will arise. Let us discuss this in “What is in Mind” section.

甜甜圈缓存用于缓存整个网页,但页面的一个或多个小部分除外。 换句话说,假设我们要缓存视图的最大部分(最小部分除外),那么我们可以最终确定使用DonutCache。 我知道在开始阅读本文时,会出现许多问题。 让我们在“注意事项”部分中对此进行讨论。

有什么想法? (What is in Mind?)



Q1: What will  happen when we use Output Caching? Q1:当我们使用输出缓存时会发生什么?

The output cache will cache the entire web page.

输出缓存将缓存整个网页。





Q2: Q2: What can we do when we want to do the inverse of the Donut Caching? 当我们想做甜甜圈缓存的逆过程时,我们该怎么办?

We want to use Donut hole cache instead of the DonutCache to cache the small portion of the view instead of maximum portion (inverse of DonutCache)

我们要使用DonutKong缓存而不是DonutCache来缓存视图的一小部分而不是最大部分(DonutCache的反函数)

何时使用Donut Cache? (When to use Donut Cache?)

Let us assume a web site that contains the home page displaying login user name (the small portion of the page that will be in dynamic), and the remainder is static content.

让我们假设一个网站包含显示登录用户名的主页(页面的一小部分将是动态的),其余部分是静态内容。

Bad Approach:

错误的方法:

If you want to cache users by using OutputCache with VaryByParam User ID, then the entire page will be cached every time for each user with a different user name. This is a bad approach because if 5000 users logged into the site, then there will be 5000 pages cached.

如果要通过将OutputCache与VaryByParam用户ID结合使用来缓存用户,则每次使用不同用户名的每个用户都将缓存整个页面。 这是一种不好的方法,因为如果5000个用户登录到该站点,则将缓存5000个页面。

Good Approach:

好的方法:

In this scenario, we can use DonutCaching. It is useful when most of the information on the page is rarely changed, with just a few items changing dynamically.

在这种情况下,我们可以使用DonutCaching。 当页面上的大多数信息很少更改,而只有少数项目动态更改时,此功能很有用。

包括使用NuGet软件包的MVCDonutCaching: (Including MVCDonutCaching using NuGet Package:)

We can install MVCDonutCaching using NuGet or using Package Manager Console, Go to Tools -> Library Package Manager -> Package Manager Console, then type the command “Install-package MvcDonutCaching” in the console as shown below image.

我们可以使用NuGet或使用程序包管理器控制台安装MVCDonutCaching转到工具->库程序包管理器->程序包管理器控制台 ,然后在控制台中键入命令“ Install-package MvcDonutCaching” ,如下图所示。

donut caching nuget - dotnet-helpers

例: (Example:)

Once we have installed the MvcDonutCaching package, we can add the DonutOutputCache attribute to the action or to the controller. Most of the parameter types of OutputCache attribute are also available in the DonutCache as shown below.

一旦安装了MvcDonutCaching软件包,就可以将DonutOutputCache属性添加到操作或控制器中。 如下所示,DonutCache中也提供了OutputCache属性的大多数参数类型。

donut caching parameters- dotnet-helpers

控制器: (Controller :)

From the below code, the static content in the Index view has cached for 30 seconds for all users except for the dynamic content which is specified inside the view.

从下面的代码中,索引视图中的静态内容已为所有用户缓存了30秒,但视图中指定的动态内容除外。

public class DonutCacheController : Controller
{
[DonutOutputCache(Duration = 30)]
public ActionResult Index()
{
ViewBag.CurrentDataTimeMessage = DateTime.Now.ToString();
return View();
}

public ActionResult LoadUserDetail()
{
ViewBag.CurrentDataTimeMessage = DateTime.Now.ToString();
return View();
}
}

Partial view:

部分视图:

Create LoadUserDetail.cshtml partial view and display the time from ViewBag. This partial view has been called in the DonutCache ->Index(view)

创建LoadUserDetail.cshtml部分视图并显示ViewBag中的时间。 该部分视图已在DonutCache-> Index(view)中调用

<h2>Message from Partial View - LoadUserDetail</h2>
@ViewBag.CurrentDataTimeMessage

View:

视图:

Here I am creating a view displaying current time and render LoadUserDetail partial view in it.

在这里,我正在创建一个显示当前时间的视图,并在其中渲染LoadUserDetail部分视图。

<h2>Index</h2>
<h2>Message from DonutCache -> Index </h2>
@ViewBag.CurrentDataTimeMessage
@Html.Action("LoadUserDetail", "DonutCache", new { name = "test" }, true)

输出: (OUTPUT:)

While running the application, when we browse DonutCache controller then it will load the index view. If we refresh the page before 30 seconds, then index view will not refresh because it has been cached by the DonutCache (Time remain the same in the view, but time from the partial view had been updated for every refresh as shown below). But LoadUserDetail has been refreshed every time because it hasn’t caught in cache as show below.

在运行应用程序时,当我们浏览DonutCache控制器时,它将加载索引视图。 如果我们在30秒之前刷新页面,则索引视图将不会刷新,因为它已被DonutCache缓存(时间在视图中保持不变,但是每次刷新时都更新了部分视图的时间,如下所示)。 但是LoadUserDetail每次都会刷新,因为它没有被捕获到缓存中,如下所示。

donut caching output-dotnet-helpers

翻译自: https://www.experts-exchange.com/articles/26319/Donut-Caching-with-Asp-Net-MVC.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值