I am wanting to compress results from QUERYS of the database before adding them to the cache.
I want to be able to compress any reference type.
I have a working
This won't work for any reference type. This will work for Serializable types. Hook up a BinaryFormatter
to a compression stream which is piped to a file:
var formatter = new BinaryFormatter();
using (var outputFile = new FileStream("OutputFile", FileMode.CreateNew))
using (var compressionStream = new GZipStream(
outputFile, CompressionMode.Compress)) {
formatter.Serialize(compressionStream, objToSerialize);
compressionStream.Flush();
}
You could use a MemoryStream
to hold the contents in memory, rather than writing to a file. I doubt this is really an effective solution for a cache, however.