2011-04-08 2 views
3
VirtualPathProvider VirtualPathProvider를 사용하여 SQL Server 테이블에서 가상 페이지를 반환합니다. 이 작업은 pk이고 내 VirtualPathProvider 클래스 파일의 코드는 아래에 있습니다.

asp.net VirtualPathProvider - GetCacheDependancy 문제를 사용하여 캐시 지우기

내가 가지고있는 문제는 데이터베이스에있는 가상 페이지 데이터 (제목 또는 페이지 텍스트)를 변경할 때 원본 페이지가 캐시 된대로 출력되는 페이지에이 변경 내용이 표시되지 않는다는 것입니다. .

VirtualPathProvider 클래스에 GetCacheDependancy를 추가하는 것에 대한 몇 가지 기사를 읽었으며 몇 가지 예제를 구현하려고 시도했지만 원본 페이지는 여전히 캐시되고 표시됩니다.

또한 가상 페이지의 페이지로드 (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) 

은 캐싱을 방지합니다. 그러나 일하는 nothings.

그래서이 캐싱 문제를 방지하기 위해 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 

최종 클래스

+0

나는 당신에 대해 물어 모르겠어요. 더 자세하게 얘기해 주 시겠어요? 귀하의 회신 알렉스 – Alex

+0

감사합니다. 원래 메시지를 편집 했으니 조금 더 명확해질 것입니다. 감사합니다 스티브 – Steve

+0

@ 스티브 당신이 해결 했습니까? –

답변

0

나는 당신이 FileExistsGetFile 방법에 대해 수행되는 것과 동일한 방식으로 가상 경로 공급자에 대한 VirtualPathProvider.GetCacheDependency 메소드를 오버라이드 (override) 할 필요가 있다고 생각합니다. 예를 들어 이 방법 : 가짜

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 방법 가상 경로가 사용자의 DB 유무에 관련이 있는지 여부를 결정해야한다. 예,이 경우 자신 만의 CacheDependency을 만들 수 있습니다 (예 : ...). 그렇지 않은 경우 모든 실제 .aspx 페이지에 Previous.GetCacheDependency을 사용할 수 있습니다.

+3

답변에 대한 자세한 내용이 필요합니다. 예를 들어 CacheDependency를 초기화하는 방법. 나는 시도했지만 성공하지 못했습니다. –