ASP.NET MVC在重写的VirtualPathProvider中禁用视图缓存

时间:2022-07-10 03:16:13

I am doing some dev work using portable areas so I have an overridden VirtualPathProvider.

我正在使用便携式区域做一些开发工作,所以我有一个重写的VirtualPathProvider。

My public override bool FileExists(string virtualPath) seems to get called only every few minutes, meaning that MVC is caching the views.

我的公共覆盖bool FileExists(字符串virtualPath)似乎每隔几分钟就被调用一次,这意味着MVC正在缓存视图。

This is probably great in production but I can't figure out how to turn it off in dev. I want the VirtualPathProvider to get called on each and every use of the view.

这可能是很好的生产,但我无法弄清楚如何在开发中关闭它。我希望在每次使用视图时调用VirtualPathProvider。

Any suggestions?

1 个解决方案

#1


6  

Answering my own question for the sake of future generations....

为了后代而回答我自己的问题....

We ended up overriding the GetCacheDependency call to ensure that the view is never cached. (We cache views manually). We had to create a FakeCacheDependency that lets us use the last modified date from our cache.

我们最终覆盖了GetCacheDependency调用,以确保视图永远不会被缓存。 (我们手动缓存视图)。我们必须创建一个FakeCacheDependency,它允许我们使用缓存中的最后修改日期。

In our application, our virtual views are called CondorVirtualFiles. (When building a views engine, you need to give it a cool name.)

在我们的应用程序中,我们的虚拟视图称为CondorVirtualFiles。 (构建视图引擎时,需要给它一个很酷的名字。)

public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            var view = this.GetFile(virtualPath);
            if (view is CondorVirtualFile)
            {
                FakeCacheDependency fcd = new FakeCacheDependency((view as CondorVirtualFile).LastModified);
                return fcd;
            }
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }



 public class FakeCacheDependency : System.Web.Caching.CacheDependency
    {
        public FakeCacheDependency(DateTime lastModified)
        {
            base.SetUtcLastModified(lastModified);
        }
        public FakeCacheDependency()
        {
            base.SetUtcLastModified(DateTime.UtcNow);  
        }
    }

#1


6  

Answering my own question for the sake of future generations....

为了后代而回答我自己的问题....

We ended up overriding the GetCacheDependency call to ensure that the view is never cached. (We cache views manually). We had to create a FakeCacheDependency that lets us use the last modified date from our cache.

我们最终覆盖了GetCacheDependency调用,以确保视图永远不会被缓存。 (我们手动缓存视图)。我们必须创建一个FakeCacheDependency,它允许我们使用缓存中的最后修改日期。

In our application, our virtual views are called CondorVirtualFiles. (When building a views engine, you need to give it a cool name.)

在我们的应用程序中,我们的虚拟视图称为CondorVirtualFiles。 (构建视图引擎时,需要给它一个很酷的名字。)

public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            var view = this.GetFile(virtualPath);
            if (view is CondorVirtualFile)
            {
                FakeCacheDependency fcd = new FakeCacheDependency((view as CondorVirtualFile).LastModified);
                return fcd;
            }
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }



 public class FakeCacheDependency : System.Web.Caching.CacheDependency
    {
        public FakeCacheDependency(DateTime lastModified)
        {
            base.SetUtcLastModified(lastModified);
        }
        public FakeCacheDependency()
        {
            base.SetUtcLastModified(DateTime.UtcNow);  
        }
    }