I coded my virtual path provider with caching, but it seems that the caching is not working.

I use this code to add the cache depedency:

public class ViewCacheDependency : CacheDependency
{
    public ViewCacheDependency(string virtualPath)
    {
        base.SetUtcLastModified(DateTime.UtcNow);
    }

    public void Invalidate()
    {
        base.NotifyDependencyChanged(this, EventArgs.Empty);
    }
}

public class ViewCacheDependencyManager
{
    private static Dictionary<string, ViewCacheDependency> dependencies = new Dictionary<string, ViewCacheDependency>();
    private static volatile ViewCacheDependencyManager instance;
    private static object syncRoot = new Object();

    private ViewCacheDependencyManager()
    {
    }

    public static ViewCacheDependencyManager Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new ViewCacheDependencyManager();
                    }
                }
            }

            return instance;
        }
    }

    public CacheDependency Get(string virtualPath)
    {
        if (!dependencies.ContainsKey(virtualPath))
            dependencies.Add(virtualPath, new ViewCacheDependency(virtualPath));

        return dependencies[virtualPath];

    }

    public void Invalidate(string virtualPath)
    {
        if (dependencies.ContainsKey(virtualPath))
        {
            var dependency = dependencies[virtualPath];
            dependency.Invalidate();
            dependency.Dispose();
            dependencies.Remove(virtualPath);
        }
    }
}

For example if my url is website.com, if I call the view from controller: /Views/Page/Home.cshtml the key that is stored in the cache is ~/Views/Page/Home.cshtml, but the problem is that I do not know is what url does my system call to check the cache?

Is it website.com/Views/Page/Home.cshtml or www.website.com/Views/Page/Home.cshtml or ~/Views/Page/Home.cshtml or what? The system always fails to get the cache

Related posts

Recent Viewed