简体   繁体   English

ASPX 文字控件缓存?

[英]ASPX Literal Control Caching?

Im adding a literal control to my ascx page我向我的 ascx 页面添加了文字控件

<asp:Literal ID="customLiteral" runat="server" />
 

Inside the Page_Load method I'm populating this text dynamically using the httpContext.在 Page_Load 方法中,我使用 httpContext 动态填充此文本。 I need to read some cookies to set the value of this Literal.我需要读取一些 cookie 来设置这个 Literal 的值。

customLiteral.Text = Utility.RenderText(HttpContext.Current) + "<h3>" + DateTime.Now.ToShortTimeString() + "</h3>";

This works perfectly fine on the first load of the page.这在第一次加载页面时非常有效。 But does not hit my RenderText method on subsequent loads, so does not honor the value of cookies to render this literal.但是在后续加载时不会命中我的 RenderText 方法,因此不尊重 cookie 的值来呈现此文字。 I added in the date value for testing and the value also remains a constant.我添加了用于测试的日期值,该值也保持不变。 Would the literal value be cached somehow?文字值会以某种方式缓存吗? I'm also hitting the breakpoints inside my Page_Load only once on the first load.我也只在第一次加载时在我的 Page_Load 内打断点一次。 Is there a better way to achieve what I want here?有没有更好的方法来实现我在这里想要的?

Based on your series of interactions with Greg, I suspect that some form of output caching is occurring.根据您与 Greg 的一系列互动,我怀疑正在发生某种形式的输出缓存。 If so, there are handful of ways to get whatever data you're trying to have presented in real-time and on each load to show up on page refresh.如果是这样,有几种方法可以实时获取您尝试呈现的任何数据,并在每次加载时显示在页面刷新时。

The key to implementation comes in the form of something called post-cache substitution ( https://docs.microsoft.com/en-us/previous-versions/ms227429(v=vs.140)?redirectedfrom=MSDN ) This technique is also known as "donut caching" because when you think about a donut, there's a hole that's cut in the middle.实现的关键是一种称为缓存后替换的形式https://docs.microsoft.com/en-us/previous-versions/ms227429(v=vs.140)?redirectedfrom=MSDN )这种技术是也称为“甜甜圈缓存”,因为当您想到甜甜圈时,会在中间切出一个洞。 To compare: if your HTML page output were the donut, the "hole" in the middle of it would be the location where you want to place your output (your Literal control) on-demand each time the page is served and not just rendered in the page lifecycle.比较:如果您的 HTML 页面输出是甜甜圈,则中间的“洞”将是您希望每次提供页面时按需放置输出(您的文字控件)的位置,而不仅仅是呈现在页面生命周期中。

Post-cache substitution can be done entirely within code, but I've preferred to use the Substitution control ( https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.substitution?view=netframework-4.8 ) as it makes things a little easier to understand.缓存后替换可以完全在代码中完成,但我更喜欢使用替换控件 ( https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.substitution? view=netframework-4.8 ),因为它使事情更容易理解。 This control consists of two logically separate parts.该控件由两个逻辑上独立的部分组成。 There's the section that responds within the normal ASP.NET page/control lifecycle (eg, Page_Load).有在正常的 ASP.NET 页面/控件生命周期(例如,Page_Load)内响应的部分。 The other portion is a public static method that gets called on the control just before the served back to a user from the output cache.另一部分是一个公共静态方法,它在从输出缓存返回给用户之前在控件上调用。 That static method is where you might serve up the contents of what you're currently attempting to show with your Literal control.您可以在该静态方法中提供您当前尝试使用 Literal 控件显示的内容。

I present at conferences and events on caching a lot, and I developed a sequence diagram to help my audiences understand what's going on:我经常在有关缓存的会议和活动中发表演讲,并开发了一个序列图来帮助我的听众了解正在发生的事情:

在此处输入图片说明

I normally work with SharePoint, and I've implemented this in numerous scenarios to great effect.我通常使用 SharePoint,并且我已经在许多场景中实现了这一点,效果非常好。 I hope it works for you!我希望这个对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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