From 13d067ea50f95ae5fe665b00429c4c0b6344093d Mon Sep 17 00:00:00 2001 From: Leszek Ciesielski Date: Thu, 7 Sep 2017 16:41:16 +0200 Subject: [PATCH] CompressingTranscoder, using DataContract serialization, for storing larger object graphs effectively --- Enyim.Caching/Enyim.Caching.csproj | 1 + .../Transcoders/CompressingTranscoder.cs | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Enyim.Caching/Memcached/Transcoders/CompressingTranscoder.cs diff --git a/Enyim.Caching/Enyim.Caching.csproj b/Enyim.Caching/Enyim.Caching.csproj index 57b7451a..995e6a16 100755 --- a/Enyim.Caching/Enyim.Caching.csproj +++ b/Enyim.Caching/Enyim.Caching.csproj @@ -291,6 +291,7 @@ + diff --git a/Enyim.Caching/Memcached/Transcoders/CompressingTranscoder.cs b/Enyim.Caching/Memcached/Transcoders/CompressingTranscoder.cs new file mode 100644 index 00000000..ca1c2b28 --- /dev/null +++ b/Enyim.Caching/Memcached/Transcoders/CompressingTranscoder.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Runtime.Serialization; + +namespace Enyim.Caching.Memcached.Transcoders +{ + public class CompressingTranscoder : DefaultTranscoder + { + protected override object DeserializeObject(ArraySegment value) + { + var ds = new NetDataContractSerializer(); + + using (var ms = new MemoryStream(value.Array, value.Offset, value.Count)) + { + using (var gs = new GZipStream(ms, CompressionMode.Decompress)) + { + return ds.Deserialize(gs); + } + } + } + + protected override ArraySegment SerializeObject(object value) + { + using (var ms = new MemoryStream()) + { + using (var gs = new GZipStream(ms, CompressionMode.Compress, true)) + { + new NetDataContractSerializer().Serialize(gs, value); + } + + return new ArraySegment(ms.GetBuffer(), 0, (int) ms.Length); + } + } + } +}