如何COM preSS使用gzip .NET对象实例 [英] How to compress a .net object instance using gzip

查看:186
本文介绍了如何COM preSS使用gzip .NET对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库的QUERYS玉米preSS结果将它们添加到缓存之前

I am wanting to compress results from QUERYS of the database before adding them to the cache.

我希望能够到COM preSS任何引用类型。

I want to be able to compress any reference type.

我有这样的COM pressing串一个工作版本。基于在Scott Hanselman的博客文章 HTTP:// shrinkster .COM /173吨

I have a working version of this for compressing strings.. the idea based on scott hanselman 's blog post http://shrinkster.com/173t

任何想法COM pressing .NET对象?

any ideas for compressing a .net object?

我知道,这将是一个只读缓存,因为在缓存中的对象只会是字节数组。

I know that it will be a read only cache since the objects in the cache will just be byte arrays..

推荐答案

这不会对任何引用类型的工作。这适用于序列化类型。钩了的BinaryFormatter 到被输送到一个文件,一个COM pression流:

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();
}

您可以使用的MemoryStream 持有的内容在内存中,而不是写入文件。我怀疑这是真正的高速缓存有效的解决方案,但是。

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.

这篇关于如何COM preSS使用gzip .NET对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆