Question

Is it possible to cache a binary file in .NET and do normal file operations on cached file?

Was it helpful?

Solution

The way to do this is to read the entire contents from the FileStream into a MemoryStream object, and then use this object for I/O later on. Both types inherit from Stream, so the usage will be effectively identical.

Here's an example:

private MemoryStream cachedStream;

public void CacheFile(string fileName)
{
    cachedStream = new MemoryStream(File.ReadAllBytes(fileName));
}

So just call the CacheFile method once when you want to cache the given file, and then anywhere else in code use cachedStream for reading. (The actual file will been closed as soon as its contents was cached.) Only thing to remember is to dispose cachedStream when you're finished with it.

OTHER TIPS

Any modern OS has a caching system built in, so in fact whenever you interact with a file, you are interacting with an in-memory cache of the file.

Before applying custom caching, you need to ask an important question: what happens when the underlying file changes, so my cached copy becomes invalid?

You can complicate matters further if the cached copy is allowed to change, and the changes need to be saved back to the underlying file.

If the file is small, it's simpler just to use MemoryStream as suggested in another answer.

If you need to save changes back to the file, you could write a wrapper class that forwards everything on to MemoryStream, but additionally has an IsDirty property that it sets to true whenever a write operation is performed. Then you can have some management code that kicks in whenever you choose (at the end of some larger transaction?), checks for (IsDirty == true) and saves the new version to disk. This is called "lazy write" caching, as the modifications are made in memory and are not actually saved until sometime later.

If you really want to complicate matters, or you have a very large file, you could implement your own paging, where you pick a buffer size (maybe 1 MB?) and hold a small number of byte[] pages of that fixed size. This time you'd have a dirty flag for each page. You'd implement the Stream methods so they hide the details from the caller, and pull in (or discard) page buffers whenever necessary.

Finally, if you want an easier life, try:

http://www.microsoft.com/Sqlserver/2005/en/us/compact.aspx

It lets you use the same SQL engine as SQL Server but on a file, with everything happening inside your process instead of via an external RDBMS server. This will probably give you a much simpler way of querying and updating your file, and avoid the need for a lot of hand-written persistence code.

Well, you can of course read the file into a byte[] array and start working on it. And if you want to use a stream you can copy your FileStream into a MemoryStream and start working with it - like:

public static void CopyStream( Stream input, Stream output )
{
        var buffer = new byte[32768];
        int readBytes;
        while( ( readBytes = input.Read( buffer, 0, buffer.Length ) ) > 0 )
        {
                output.Write( buffer, 0, readBytes );
        }
}

If you are concerned about performance - well, normally the build-in mechanisms of the different file access methods should be enough.

I don't know what exactly you're doing, but I offer this suggestion (which may or may not be viable depending on what you're doing):

Instead of only caching the contents of the file, why don't you put the contents of the file in a nice strongly typed collection of items, and then cache that? It'll probably make searching for items a bit easier, and faster since there is no parsing involved.

There is a very elegant caching system in Lucene that caches bytes from the disk into memory and intelligently updates the store etc. You might want to have a look at that code to get an idea of how they do it. You might also want to read up on the Microsoft SQL Server data storage layer - as the MSSQL team is pretty forthcoming about some of the more crucial implementation details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top