Virtual Path Provider disable caching?

∥☆過路亽.° 提交于 2019-11-30 09:23:48

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

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

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.

user2095150
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);
}

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!