2012-04-20 24 views
0

我們在我們的asp.net(vb)web應用程序中使用zedgraph webcontrols繪製圖表。生成圖像的臨時目錄填滿,現在已達到2GB +。如何自動清除zedgraph圖像的臨時目錄?

有沒有辦法自動清除此目錄或我必須實施一個討厭的HttpContext.Cache expired callback黑客從那裏刪除舊文件?

感謝,弗洛

回答

1

所以,與其讓我的手髒的編輯和重新編譯zedgraphweb http://sourceforge.net/projects/zedgraph,我選擇了一個比較常見的apporach,處理文件的自己的量。我正在使用Cache的CacheItemRemovedCallback在Web應用程序中執行代碼,例如使用計時器。此代碼重新填充緩存項目並因此更新週期。瞧:磁盤清理每5分鐘在一個Web應用程序。

Public Module Maintenance 
    Private Const triggerKey As String = "MaintenanceDiskCleaner" 
    Friend Sub Run() 
     Try 
      SyncLock triggerKey 
       If Hosting.HostingEnvironment.Cache(triggerKey) Is Nothing Then 
        Hosting.HostingEnvironment.Cache.Add(triggerKey, _ 
                DateTime.Now, _ 
                Nothing, _ 
                DateTime.Now.AddMinutes(5), _ 
                Cache.NoSlidingExpiration, _ 
                CacheItemPriority.Default, _ 
                AddressOf CacheItemRemovedCallback) 
       End If 
      End SyncLock 
     Catch ex As Exception 

     End Try 
    End Sub 

    Public Sub CacheItemRemovedCallback(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason) 
     Try 
      Dim dir As String = Hosting.HostingEnvironment.MapPath("~\ZedGraphImages") 
      If Directory.Exists(dir) Then 

       SyncLock triggerKey 
        Dim di As DirectoryInfo = New DirectoryInfo(dir) 
        Dim files As FileSystemInfo() = di.GetFileSystemInfos() 
        For Each file As FileSystemInfo In From f In files Where f.CreationTime < CType(value, DateTime) 
         Try 
          WebTools.Files.TryDelete(file.Name, 50) 
         Catch ex As Exception 

         End Try 
        Next 
       End SyncLock 

      End If 
      Run() 
     Catch ex As Exception 

     End Try 
    End Sub 
End Module