File Caching-VBForums
Results 1 to 4 of 4

Thread: File Caching

  1. #1

    Thread Starter
    Banned plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    File Caching

    The following code was born out of boredom on the DART this morning somewhere between Sydney Parade and Blackrock on my way to work. Its an enhanced version of my original file caching code that uses Cache Dependencies. Basically, if the file you've cached changes ASP.NET will remove the file from cache, so that the code can reload it on its next call.

    VB Code:
    1. Public Function strGetFile(ByVal strFilePath As String) As String
    2.     Dim strRetVal As String = Cache(strFilePath)
    3.     If strRetVal = vbNullString Then
    4.         Response.Write("(Not Cached)")
    5.         Dim objStreamReader As StreamReader = File.OpenText(strFilePath)
    6.         strRetVal = objStreamReader.ReadToEnd()
    7.         objStreamReader.Close()
    8.         Dim objDepend As New Caching.CacheDependency(strFilePath)
    9.         Cache.Insert(strFilePath, strRetVal, objDepend)
    10.     Else
    11.         Response.Write("(From Cache)")
    12.     End If
    13.     Return strRetVal
    14. End Function

    To demonstrate the usefulness of this code, try it out with a very large file, with code something like this:

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Response.Write("Start Time : " & Now & " ... ")
    3.     strGetFile("c:\inetpub\wwwroot\WebApplication1\my_150_meg_file.txt")
    4.     Response.Write("End Time : " & Now & " ... ")
    5. End Sub

    Simply create some large file somewhere, ensure the ASP.NET worker process has access to the folder and then browse to your ASPX page. Unless your system is lighning fast you'll see some delay before the page is displayed to the browser. ASP.NET has just loaded the file from the drive and cached it. You'll also see the time difference. Now try refreshing the page. ASP.NET has just loaded the file directly from cache.
    If you modify the contents of the file in any way ASP.NET will remove it from cache and as such cause it to be reloaded from the drive on the next call.

    The advantages and disadvantages are obvious. Advantages being faster page execution, and also extension of the lifespan of your disk drives. Disadvantage being that of memory usage - but hey, with no server shipping with less than 1GB of RAM these days, is that really an issue?

    Happy Coding

  2. #2

    Thread Starter
    Banned plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    For any generic purpose use:

    VB Code:
    1. Public Function strGetFile(ByVal strFilePath As String) As String
    2.     Dim strRetVal As String = Cache(strFilePath)
    3.     If strRetVal = vbNullString Then
    4.         Dim objStreamReader As StreamReader = File.OpenText(strFilePath)
    5.         strRetVal = objStreamReader.ReadToEnd()
    6.         objStreamReader.Close()
    7.         Cache.Insert(strFilePath, strRetVal, New Caching.CacheDependency(strFilePath))
    8.     End If
    9.     Return strRetVal
    10. End Function

  3. #3
    New Member
    Join Date
    Mar 2005
    Posts
    1

    Re: File Caching

    well, try this one perhaps...

  4. #4

    Thread Starter
    Banned plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: File Caching

    ... yes... indeed...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.