如何将 ASP.NET 菜单控件放入缓存中?
我有一个 SharePoint2010 网站,我已为其创建了 ASP.NET 菜单控件。 ASP.NET 菜单的内容最初是空的,在 Page_Load 上我从服务器上的标准 HTML 文件加载其内容:
protected void Page_Init(object sender, EventArgs e)
{
string MenuPath = (string)ConfigurationSettings.AppSettings["RootMenuPath"].ToString();
Menu1.Items[0].ChildItems[0].Text = File.ReadAllText(MenuPath + "\\About.htm");
//etc...
}
我注意到这是一种可怕的做法。每次用户加载页面时它都会访问磁盘。
我怎样才能:
a) 缓存代码和 asp.net 菜单项,以便它保留在内存中?
b) 使用另一种方法来确保它不是从磁盘加载?
谢谢
I have a SharePoint2010 site that I have created an ASP.NET menu control for.
The ASP.NET menu's content is initially empty, and on Page_Load I load its content from standard HTML files on the server:
protected void Page_Init(object sender, EventArgs e)
{
string MenuPath = (string)ConfigurationSettings.AppSettings["RootMenuPath"].ToString();
Menu1.Items[0].ChildItems[0].Text = File.ReadAllText(MenuPath + "\\About.htm");
//etc...
}
I notice this is a horrible way to do things. It hits the disk every single time a user loads a page.
How can I either:
a) Cache the code and asp.net menu item so that it stays in memory?
b) Use another method to ensure it isn't loaded from the disk?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将数据加载包装到属性中并至少使用页面缓存:
You can wrap data load into property and use at least page Cache there:
您确实可以使用缓存或某些仅在
Application_Start
中初始化一次并稍后重用的变量,但我担心您在这里做了一些过早的优化。除非您确定这是应用程序性能的瓶颈,否则您可能不应该这样做。从磁盘读取文件是一个快速的操作,尤其是当它们很小时。You could indeed use the cache or some variable that is initialized only once in
Application_Start
and reused later but I am afraid that you are doing some premature optimization here. You probably shouldn't be doing it unless you have identified that this is a bottleneck for your application performance. Reading files from disk is a fast operation especially if they are small.如果可能的话,我会将菜单数据存储在 XML 文件中,并缓存该 XML 文件。
If possible, I would store the menu data in an XML file, and cache the XML file.