Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual Path Provider disable caching?

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?

like image 567
downatone Avatar asked Oct 01 '09 18:10

downatone


3 Answers

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);
    }
like image 111
Samir Seth Avatar answered Sep 28 '22 20:09

Samir Seth


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);
    }
like image 23
Chandima Prematillake Avatar answered Sep 28 '22 20:09

Chandima Prematillake


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.

like image 31
jrichview Avatar answered Sep 28 '22 21:09

jrichview