How to compress a .net object instance using gzip

前端 未结 4 1753
执笔经年
执笔经年 2021-02-10 11:49

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

4条回答
  •  悲&欢浪女
    2021-02-10 12:43

    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.

提交回复
热议问题