虚拟路径提供程序禁用缓存?

发布于 2024-08-06 14:03:21 字数 350 浏览 9 评论 0原文

我有一个虚拟路径提供者。问题是它缓存我的文件。每当我手动编辑它引用的 aspx 文件之一时,VPP 不会提取新文件,它会继续重用旧文件,直到我重新启动站点。

我什至在我的 VirtualPathProvider 类中重写了 GetCacheDependency():

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }

想法?

I have a virtual path provider. Problem is its caching my files. Whenever I manually edit one of the aspx files it references the VPP doesn't pull in the new file, it continues to reuse the old file until I restart the site.

I've even over-rode the GetCacheDependency() in my VirtualPathProvider class:

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }

Ideas?

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

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

发布评论

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

评论(5

仙女山的月亮 2024-08-13 14:03:21

返回 null 本质上是告诉 ASP.NET 您没有任何依赖项 - 因此 ASP.NET 不会重新加载该项目。

您需要的是返回有效的依赖项,例如,

 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return new CacheDependency(getPhysicalFileName(virtualPath));
    }

更正确的方法是确保您只处理自己的缓存依赖项(这是一个示意性示例):

 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (isMyVirtualPath(virtualPath))
            return new CacheDependency(getPhysicalFileName(virtualPath));
        else
            return new Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }

Returning a null is essentially telling ASP.NET that you do not have any dependency - hence ASP.NET will not reload the item.

What you need is to return a valid dependency e.g.

 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return new CacheDependency(getPhysicalFileName(virtualPath));
    }

A more correct approach is to make sure that you only handle your own cache dependencies (this is a schematic example) :

 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (isMyVirtualPath(virtualPath))
            return new CacheDependency(getPhysicalFileName(virtualPath));
        else
            return new Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }
半透明的墙 2024-08-13 14:03:21

禁用缓存的正确方法是:

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (_IsLayoutFile(virtualPath))
        {
            return null;
        }
        return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }

    public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies)
    {
        if (_IsLayoutFile(virtualPath))
        {
            return Guid.NewGuid().ToString();
        }

        return Previous.GetFileHash(virtualPath, virtualPathDependencies);
    }

The correct way to disable caching is this:

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (_IsLayoutFile(virtualPath))
        {
            return null;
        }
        return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }

    public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies)
    {
        if (_IsLayoutFile(virtualPath))
        {
            return Guid.NewGuid().ToString();
        }

        return Previous.GetFileHash(virtualPath, virtualPathDependencies);
    }
┊风居住的梦幻卍 2024-08-13 14:03:21

我不相信这就是原发帖者所问的。他想完全禁用缓存,而不是以更好的方式实现它,尽管您的帖子对后者有帮助。

许多人使用 VirtualPathProvider 从数据库而不是文件系统中提取数据。我不明白创建文件系统依赖项如何成为确定何时刷新文件的有用方法。

您如何强制它从不使用缓存并始终检索文件的最新版本?这就是问题所在。

I don't believe this is what the original poster asked. He wants to disable the caching entirely, not implement it in a better way, although your post is helpful for the latter.

A great many people are using VirtualPathProvider to pull data from a database instead of a file system. I don't see how creating a file system dependency would be a useful way to determine when it's time to refresh the file.

How would you FORCE it to never use caching and always retrieve the latest version of the file? That is the question.

雪落纷纷 2024-08-13 14:03:21
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
   return IsVirtualPath(virtualPath) ? new CacheDependency(HttpContext.Current.Server.MapPath("~/Resource.xml")) 
                                     : Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
   return IsVirtualPath(virtualPath) ? new CacheDependency(HttpContext.Current.Server.MapPath("~/Resource.xml")) 
                                     : Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
神回复 2024-08-13 14:03:21

根据需要为我工作的解决方案是:

  • GetCacheDependency: return null;
  • GetFileHash:返回Guid.NewGuid().ToString();

但是,使用此解决方案会导致服务器挂起(Cassini、IIS 6、IIS 7、IIS 8)。悬挂只持续几分钟,然后结果就出来了。

我还对虚拟路径/文件进行了测试,结果相同。我搞乱了浏览器超时。

有人可以帮忙吗?

The solution that worked for me as desired was:

  • GetCacheDependency: return null;
  • GetFileHash: return Guid.NewGuid().ToString();

However, with this solution results in hanging the server (Cassini, IIS 6, IIS 7, IIS 8). The hanging only lasts for a few minutes then the results are delivered.

I've also included a test for virtual path/file with the same results. I messed with browser timeouts.

Can anyone help?

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