简体   繁体   English

在ASP.NET中缓存用户控件?

[英]Caching a user control in ASP.NET?

I have created a user control in my application "header.ascx", I am pasing a selectedMenu attribute to this control on which the control selects the selectedMenu value specified. 我在我的应用程序“header.ascx”中创建了一个用户控件,我将一个selectedMenu属性设置为此控件,控件在该控件上选择指定的selectedMenu值。 Suppose, I have passed value "home" or "search" then it will select (highlight it) the search menu. 假设,我已经通过了值“home”或“search”,那么它将选择(突出显示)搜索菜单。

I want to cache this control, When the value of the selectedMenu attribute changes then only the cache will be refreshed else it should picks up the control from cache?? 我想缓存这个控件,当selectedMenu属性的值发生变化时,只刷新缓存,否则它应该从缓存中获取控件?

Is it possible to cache a user control in asp.net?? 是否可以在asp.net中缓存用户控件? I am using ASP.NET 2.0 (C#) 我使用的是ASP.NET 2.0(C#)

User control caching in ASP.NET is called fragment caching . ASP.NET中的用户控件缓存称为片段缓存 It's done by adding an OutputCache directive to the top of your page: 这是通过在页面顶部添加一个OutputCache指令来完成的:

You can't vary the cache by setting the property on the control because the control isn't actually created if it's found in the cache. 您无法通过在控件上设置属性来更改缓存,因为如果在缓存中找到控件,则实际上不会创建控件。 If you try to access the control in the code behind it's cached, it will be null. 如果您尝试在其缓存后面的代码中访问该控件,则它将为null。

Is the condition that determines whether the control should be cached or not something that you can determine by looking at the current request? 判断是否应该缓存控件的条件是否可以通过查看当前请求来确定? If it is, you can use the varybycustom attribute ( http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx ) of the output cache directive. 如果是,则可以使用输出缓存指令的varybycustom属性( http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx )。 You can put any string you want in there as the parameter and then when the caching is evaluated the GetVaryByCustomString() method from Global.asxa will be called and you can put the logic for whether the control should be cached or not there. 您可以将所需的任何字符串作为参数放入,然后在评估缓存时,将调用Global.asxa中GetVaryByCustomString()方法,并且可以设置是否应该缓存控件的逻辑。

Of course you can! 当然可以! It's called "Fragment Caching". 它被称为“片段缓存”。 Here's a link to the Quickstarts page and the MS Knowledge base . 这是Quickstarts页面MS知识库的链接。 Additionally, Google . 此外, 谷歌

I don't think it's a good idea to cache the control itself: 我不认为缓存控件本身是个好主意:

  • When the control is created the very first time, it has references to its parent page among others. 第一次创建控件时,它会引用其父页面等。
  • When you retrieve the control from the cache, those references does no longer exists. 从缓存中检索控件时,这些引用不再存在。

A better approach, I think, is to cache the data which the control is using instead. 我认为,更好的方法是缓存控件正在使用的数据。 ASP.NET creates so many controls during the page life cycle, that caching this one control really doesn't improve anything. ASP.NET在页面生命周期中创建了很多控件,缓存这一个控件实际上并没有改进任何东西。

Then a stupid question at the end: Is this control a bottleneck? 然后是一个愚蠢的问题:这个控制是一个瓶颈吗? Do you really need the cache? 你真的需要缓存吗?

To summarize 总结一下

using VaryByCustom, means 使用VaryByCustom,意思是

1- Build the control again. 1-再次构建控件。
2- Having multiple versions of the control in the cache. 2-在缓存中有多个版本的控件。 which will be used when the custom conditions meet. 将在自定义条件满足时使用。

This is only good if the control is taking a lot of time to build and we have finite number of cached versions to not waste memory, and the application will need to access control properties (while it is cached "or null"). 这只有在控件花费大量时间构建并且我们拥有有限数量的缓存版本以避免浪费内存时才有用,并且应用程序将需要访问控件属性(当它被缓存为“或null”时)。

but it will not be good if that custom conditions are depending on the control properties itself. 但是,如果自定义条件取决于控件属性本身,那就不好了。 I can't access it, it is null. 我无法访问它,它是null。

for example I want to write something like if (the default selected value in countries list is NOT USA) then rebuild and Cache (give it a different string) 例如,我想编写类似if(在国家/地区列表中的默认选定值不是美国)然后重建和缓存(给它一个不同的字符串)

Otherwise don't 否则不要

while other objects are trying to access the contries list, it will check for null, and set the countries drop down list to USA. 当其他对象试图访问contries列表时,它将检查null,并将countries下拉列表设置为USA。

The data cahing will do the work. 数据cahing将完成工作。 it is the only way. 这是唯一的方法。

who agree? 谁同意?

Thanks for ur time 谢谢你的时间

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

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