C# 在 ASP.NET 中缓存用户控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/568837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caching a user control in ASP.NET?
提问by Prashant
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. Suppose, I have passed value "home" or "search" then it will select (highlight it) the search menu.
我在我的应用程序“header.ascx”中创建了一个用户控件,我将 selectedMenu 属性传递给该控件,该控件在该控件上选择指定的 selectedMenu 值。假设,我传递了值“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?? I am using ASP.NET 2.0 (C#)
是否可以在 asp.net 中缓存用户控件?我正在使用 ASP.NET 2.0 (C#)
回答by Cerebrus
Of course you can! It's called "Fragment Caching". Here's a link to the Quickstarts pageand the MS Knowledge base. Additionally, Google.
回答by Helephant
User control caching in ASP.NET is called fragment caching. It's done by adding an OutputCache directive to the top of your page:
ASP.NET 中的用户控件缓存称为片段缓存。它是通过在页面顶部添加一个 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.
您不能通过在控件上设置属性来改变缓存,因为如果在缓存中找到该控件,则实际上并未创建该控件。如果您尝试访问缓存背后的代码中的控件,它将为空。
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. You can put any string you want in there as the parameter and then when the caching is evaluated the GetVaryByCustomString() method from Global.asxawill be called and you can put the logic for whether the control should be cached or not there.
是否可以通过查看当前请求来确定是否应该缓存控件的条件?如果是,您可以使用输出缓存指令的变量自定义属性 ( http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx)。您可以将您想要的任何字符串作为参数放入其中,然后在评估缓存时,将调用Global.asxa 中的GetVaryByCustomString() 方法,您可以将是否应缓存控件的逻辑放在那里。
回答by Thomas Eyde
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?
那么最后一个愚蠢的问题:这个控制是一个瓶颈吗?你真的需要缓存吗?
回答by Costa
To summarize
总结一下
using VaryByCustom, means
使用 VaryByCustom,意味着
1- Build the control again.
2- Having multiple versions of the control in the cache. which will be used when the custom conditionsmeet.
1- 再次构建控件。
2- 在缓存中有多个版本的控件。当自定义条件满足时将使用。
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").
这仅在控件花费大量时间构建并且我们有有限数量的缓存版本不会浪费内存时才有用,并且应用程序将需要访问控件属性(当它被缓存为“或空”时)。
but it will not be good if that custom conditionsare depending on the control properties itself. I can't access it, it is 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.
当其他对象试图访问国家列表时,它将检查是否为空,并将国家下拉列表设置为美国。
The data cahing will do the work. it is the only way.
数据缓存将完成这项工作。这是唯一的方法。
who agree?
谁同意?
Thanks for ur time
谢谢你的时间