2009-10-01 67 views
7

我有一个虚拟路径提供程序。问题是它缓存我的文件。无论何时我手动编辑其中一个aspx文件,它引用的VPP都不会引入新文件,它会继续重新使用旧文件,直到我重新启动该网站。虚拟路径提供程序禁用缓存?

我甚至在我的VirtualPathProvider类过度骑着GetCacheDependency():

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

想法?

回答

19

返回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); 
    } 
+1

更进一步的正确答案/ Chandima Prematillake – r3mark 2013-06-11 04:33:24

1
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); 
} 
2

我不相信这是个什么原始海报问。他希望完全禁用缓存,而不是以更好的方式实现缓存,尽管您的文章对后者有帮助。

很多人使用VirtualPathProvider从数据库中取出数据,而不是从文件系统中取出数据。我没有看到如何创建文件系统依赖关系是确定何时刷新文件的有用方法。

你会如何强制它永远不会使用缓存并始终检索最新版本的文件?就是那个问题。

+0

回复此处:) http://stackoverflow.com/questions/3747858/asp-net-mvc-2-virtualpathprovider-getfile-every-time-for-every-请求/ 3766321#3766321 – Aliz 2015-02-13 11:19:47

14

禁用缓存正确的方法是这样的:

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); 
    } 
+0

根据假设,返回null缓存依赖将强制VPP使用getfilehash? – 2013-11-06 08:44:09

+0

我想通了,这是真的:返回一个null CacheDependecy将强制VPP使用GetFileHash。 – 2014-03-08 06:32:11

-1

到,根据要求为我工作的解决方案是:

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

但是,使用此解决方案导致挂起服务器(Cassini,IIS 6,IIS 7,IIS 8)。吊只持续几分钟,然后结果交付。

我还包括一个测试虚拟路径/文件与相同的结果。我迷惑浏览器超时。

任何人都可以帮忙吗?

+0

你应该问一个新问题。不要在答案中提问。 – Difster 2017-07-25 23:10:44

+0

好。在这里问:https:// stackoverflow。COM /问题/ 45320336 /的VirtualPathProvider-禁用缓存,挂起服务器-IIS-和卡西尼 – jbecker 2017-07-28 02:14:07