2012-04-20 77 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