为什么一个 URL 上的页面使用另一个 URL 中的缓存页面?

发布于 2024-10-05 03:33:50 字数 1071 浏览 1 评论 0原文

背景

我对 ASP.NET 很陌生,对缓存的概念也很陌生。我希望对于那些知道自己在做什么的人来说,这里的问题会很明显。

使用 MCMS 2002,我构建了一个 template.aspx 页面,该页面接收“person_id”查询字符串并相应地构建页面。我工作的学校的几个不同部门正在使用此模板来显示教师传记。这一切都工作正常。

问题

当我尝试缓存页面时,问题就开始发生。当一个人在一个部门的页面上访问 person_id=16175,然后在不同的部门页面上访问相同的数字简历时,它会加载缓存的页面而不是重建它。那么,问题就在于它的部门品牌都是错误的。例如:

http://health.usf.edu/medicine/妇产科/facstaf/profiles.htm?person_id=16175 http://health.usf.edu/medicine/surgery/surgery_bios.html ?person_id=16175

person_id 相同,但 URL 明显不同。如果模板能够识别不同的 URL 并忽略缓存,那就太好了。我假设问题在于两个页面都是在后端使用相同的 aspx 页面构建的。这是来自 aspx 页面的 OutputCache 位:

<%@ OutputCache Duration="86400" Location="Server" VaryByParam="person_id; section" VaryByCustom="CMSPosting" VaryByHeader="Referer" %>

我认为我在这里做错了什么。希望这对于那些知道他们在做什么的人来说是显而易见的。如果您需要更多信息,请随时询问。谢谢!

Background

I'm pretty new to ASP.NET and very new to the concept of caching. I'm hoping that for someone who knows that they're doing, the problem here will be obvious.

Using MCMS 2002, I built a template.aspx page that receives a "person_id" query string and builds pages accordingly. Several different departments at the school where I work are using this template to display faculty biographies. This much is all working correctly.

The Problem

The problems start happening when I try caching the page. When a person visits person_id=16175 on one department's page, then visits the same numeric bio on a different department page, it loads the cached page instead of rebuilding it. The problem, then, is that it has all of the wrong department branding. For example:

http://health.usf.edu/medicine/obgyn/facstaf/profiles.htm?person_id=16175
http://health.usf.edu/medicine/surgery/surgery_bios.html?person_id=16175

It's the same person_id, but the URLs are obviously different. It would be great if the template would recognize the different URLs and ignore the cache. I'm assuming that the problem lies in the fact that both pages are being built using the same aspx page on the backend. Here's the OutputCache bit from the aspx page:

<%@ OutputCache Duration="86400" Location="Server" VaryByParam="person_id; section" VaryByCustom="CMSPosting" VaryByHeader="Referer" %>

I assume I'm doing something wrong here. Hopefully it will be obvious to someone who knows that they're doing. If you need more info, don't hesitate to ask. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2024-10-12 03:34:03

您的假设很可能是正确的,因为它们使用相同的 ASPX 文件,我也有过同样的亲身经历。要解决此问题,请尝试以下操作。

在您的输出缓存指令中尝试此操作

<%@ OutputCache Location="Server" VaryByCustom="url" Duration="60" VaryByParam="*" %>

,然后将其添加到您的 Global.asax 文件中。

public override string GetVaryByCustomString(HttpContext context,string arg)
{
    if (arg == "url")
    {
         return context.Request.RawUrl;
    }
    return context.Request.RawUrl;//you can vary by other stuff here
}

对于给定参数的方法返回的每个唯一值,都会缓存页面的版本。因此,通过返回 URL,您可以确保缓存不会被不同的 URL 命中。

需要注意的关键一点是,如果您没有解析为多个 URL 的单个物理文件,则 VaryByParam="*" 将足以获得您想要的行为。这是因为您有此 URL 路由,因此需要 VaryByCustom="url"

Your are most likely correct in your assumption about it being because they use the same ASPX file, I have experienced the same firsthand. To get around this issue try the following.

Try this for your output cache directive

<%@ OutputCache Location="Server" VaryByCustom="url" Duration="60" VaryByParam="*" %>

and then add this in your Global.asax file.

public override string GetVaryByCustomString(HttpContext context,string arg)
{
    if (arg == "url")
    {
         return context.Request.RawUrl;
    }
    return context.Request.RawUrl;//you can vary by other stuff here
}

A version of the page will be cached for every unique value returned by the method for a given argument. So by returning the URL you ensure the cache will not be hit for different URLs

A key thing to note is that if you do not have a single physical file resolving to multiple URLs then VaryByParam="*" would be enough to get your desired behaviour. It's because you have this URL routing that you require VaryByCustom="url"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文