首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >asp.net VirtualPathProvider -使用GetCacheDependancy清除缓存问题

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

Stack Overflow用户
提问于 2011-04-08 17:56:37
回答 1查看 1.4K关注 0票数 3

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

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

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

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

代码语言:javascript
复制
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类文件进行一些更改,以防止这些缓存问题。感谢您能提供的任何帮助!

代码语言:javascript
复制
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

End Class
EN

回答 1

Stack Overflow用户

发布于 2011-04-08 20:49:47

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

代码语言:javascript
复制
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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5593421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档