asp.net VirtualPathProvider - 使用GetCacheDependancy问题清除缓存

时间:2011-04-08 09:56:37

标签: asp.net caching clear virtualpathprovider

我正在使用VirtualPathProvider从SQL Server表返回虚拟页面。这是工作pk,我的VirtualPathProvider类文件中的代码如下所示。

我遇到的问题是,当我更改数据库中保存的虚拟页面数据(标题或页面文本)时,此更改不会显示在原始页面已缓存的输出页面上。

我已经阅读了一些关于将GetCacheDependancy添加到VirtualPathProvider类的文章,我尝试了几个示例,但原始页面仍然被缓存并显示。

我还尝试在虚拟页面的页面加载中添加一些代码(Response.AddCacheItemDependency("Pages"))并编辑global.asax:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        HttpContext.Current.Cache.Insert("Pages", DateTime.Now, Nothing, _
             System.DateTime.MaxValue, System.TimeSpan.Zero, _
             System.Web.Caching.CacheItemPriority.NotRemovable, _
             Nothing)

防止缓存。但是没有工作。

所以我所追求的是VirtualPathProvider类文件的一些更改,以防止这些缓存问题。感谢您提供的任何帮助!

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.Hosting
Imports System.Web.UI.MobileControls
Imports System.Collections.Generic

Public Class DbVirtualPathProvider
    Inherits VirtualPathProvider
    Public Shared Sub AppInitialize()
        Dim db As New DbVirtualPathProvider()
        HostingEnvironment.RegisterVirtualPathProvider(db)
    End Sub

Public Overrides Function FileExists(ByVal virtualPath As String) As Boolean
    Dim strConn As String = ConfigurationManager.ConnectionStrings("LIQUIDConnectionString").ConnectionString
    Dim cnn As New SqlConnection(strConn)
    cnn.Open()
    Dim cmd As New SqlCommand()
    cmd.Connection = cnn
    cmd.CommandText = "select count(*) from tbl_VirtualFiles where virtualpath='" & virtualPath & "'"
    Dim retval As Object = cmd.ExecuteScalar()
    cnn.Close()
    Dim i As Integer = Convert.ToInt32(retval)
    If i <= 0 Then
        'important as if no virtual file it looks for physical file
        Return Previous.FileExists(virtualPath)
    Else
        Return True
    End If
End Function

Public Overrides Function GetFile(ByVal virtualPath As String) As VirtualFile
    Dim file As New DbVirtualFile(virtualPath)
    If file.WebFormContent Is Nothing Then
        Return Previous.GetFile(virtualPath)
    Else
        Return file
    End If
End Function

结束班

1 个答案:

答案 0 :(得分:0)

我认为您需要以与FileExistsGetFile方法相同的方式覆盖虚拟路径提供程序的VirtualPathProvider.GetCacheDependency方法。所以它可以做到,例如这样:

Public Overrides Function GetCacheDependency(virtualPath As String, virtualPathDependencies As IEnumerable, utcStart As DateTime) As CacheDependency
    If IsVirtualPathForDatabase(virtualPath) Then
        Return New CacheDependency(...)
    Else
        Return New Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart)
    End If
End Function

假的IsVirtualPathForDatabase方法应该确定虚拟路径是否与您的数据库相关。如果是,您可以为此创建自己的CacheDependency(我为其留出一个位置...),否则您可以对所有实际Previous.GetCacheDependency页面使用.aspx