如何COM preSS使用gzip .NET对象实例实例、对象、preSS、COM

2023-09-03 11:35:37 作者:珍爱生命远离网络

我想从数据库的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.