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
What sort of objects are you putting in the cache? Are they typed objects? Or things like DataTable
? For DataTable
, then perhaps store as xml compressed through GZipStream
. For typed (entity) objects, you'll probably need to serialize them.
You could use BinaryFormatter
and GZipStream
, or you could just use something like protobuf-net serialization (free) which is already very compact (adding GZipStream
typically makes the data larger - which is typical of dense binary). In particular, the advantage of things like protobuf-net is that you get the reduced size without having to pay the CPU cost of unzipping it during deserialization. In some tests before adding GZipStream
, it was 4 times faster than BinaryFormatter
. Add the extra time onto BinaryFormatter
for GZip and it should win by a considerable margin.