Can I Store whole Files in Memory (RAM Disk or Cache)?

Jim Webb 41 Reputation points
2022-03-01T12:05:14.87+00:00

I am writing an application which needs very fast file access - reading from disk can be "slow" depending on the disk type. A RAM disk would be ideal for temporary storage but I have not found any way to set one up. Can someone point me in a direction where I can learn how to do this (or even show me). Failing that, can a memory cache be used in a similar way to temporarily store whole files? If so, how?

All the best,
Jim.

Developer technologies | VB
{count} votes

4 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-03-01T12:10:29.593+00:00

    With Memory Mapped files : MemoryMappedFile Class


  2. WayneAKing 4,931 Reputation points
    2022-03-01T20:43:00.27+00:00

    I'm tempted to try the RAM disk route if there are any suggestions.

    Some sources with which to experiment:

    List of RAM drive software
    https://en.wikipedia.org/wiki/List_of_RAM_drive_software

    The last Windows suggestion might be worth trying first:

    "Native
    Windows also has a rough analog to tmpfs in the form of
    "temporary files". Files created with both
    FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_DELETE_ON_CLOSE
    are held in memory and only written to disk if the system
    experiences high memory pressure."

    Also check out:

    "ImDisk Virtual Disk Driver is a disk image emulator created
    by Olof Lagerkvist. It is free and open-source software, and
    is available in 32- and 64-bit variants. "

    See the overview of this utility here:

    How to Set up and Use a Ram Drive in Windows 10
    https://www.maketecheasier.com/setup-ram-disk-windows/

    • Wayne

  3. Dewayne Basnett 1,381 Reputation points
    2022-03-02T18:02:13.207+00:00

    I would create an object that had a path to the mht file and a string for the document text. As part of the decompression process I'd create objects for each of the files and store them in a dictionary based on page name.

    When browsing I'd check to see if the document text was empty. If not just set the webrowser's document text from that, if it was empty I'd navigate to the path and when the navigation was complete I'd set the document text in the object.

    This way it is only slow the first time a page is accessed.


  4. Dewayne Basnett 1,381 Reputation points
    2022-03-03T18:39:00.773+00:00

    This shows what I mentioned earlier,

    Public Class Form1
    
        Private myMHT As MHTFiles
        Private initedMHT As Boolean = False
        Private currentItem As MHTFiles.MHTf
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            'p has foler with mht / mhtml files
            Dim p As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            p = IO.Path.Combine(p, "MHT")
            My.Settings.MHTPath = p
    
            myMHT = New MHTFiles(p)
    
            ComboBox1.DataSource = myMHT.Names
            ComboBox1.SelectedIndex = -1
            initedMHT = True
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Try
                ' IO.Directory.Delete(My.Settings.MHTPath, True)
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
                                                   e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If Not initedMHT Then Exit Sub
            'get an item
            currentItem = myMHT.Item(ComboBox1.SelectedItem.ToString)
            If currentItem IsNot Nothing Then
                naviLock.WaitOne() 'browser wait lock
                If currentItem.Navigate Then
                    'first time navigate, see WebBrowser1_DocumentCompleted
                    WebBrowser1.Navigate(currentItem.path)
                Else
                    'was previously navigated
                    WebBrowser1.DocumentText = currentItem.docText
                    naviLock.Set()
                End If
            End If
        End Sub
    
        Private naviLock As New Threading.AutoResetEvent(True)
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            If currentItem.Navigate Then
                currentItem.docText = WebBrowser1.DocumentText
            End If
            naviLock.Set() 'set the lock
        End Sub
    End Class
    
    Public Class MHTFiles
        Private Dict As New Dictionary(Of String, MHTf)
    
        Public Sub New(DirWithMHTfiles As String)
    
            Dim fs() As String = IO.Directory.GetFiles(DirWithMHTfiles)
            Dim mht As IEnumerable(Of String)
            mht = From fn In fs
                    Let ext = IO.Path.GetExtension(fn).ToLower.TrimStart("."c)
                    Where ext = "mhtml" OrElse
                            ext = "mht"
                    Select fn
    
            For Each f As String In mht
                Dim d As New MHTf
                d.path = f
                Me.Dict.Add(IO.Path.GetFileNameWithoutExtension(f), d)
            Next
        End Sub
    
        Public Function Item(ItemName As String) As MHTf
            Dim rv As MHTf
            If Me.Dict.ContainsKey(ItemName) Then
                rv = Me.Dict(ItemName)
            End If
            Return rv
        End Function
    
        Public Function Names() As List(Of String)
            Return Me.Dict.Keys.ToList
        End Function
    
        Public Class MHTf
            Public Function Navigate() As Boolean
                Return Me.docText = ""
            End Function
    
            Public path As String
            Public docText As String = ""
        End Class
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.