Skip to content
This repository has been archived by the owner on Jul 7, 2019. It is now read-only.

CompressingTranscoder #177

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Enyim.Caching/Enyim.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
</Compile>
<Compile Include="Memcached\Transcoders\CacheItem.cs">
</Compile>
<Compile Include="Memcached\Transcoders\CompressingTranscoder.cs" />
<Compile Include="Memcached\Transcoders\DataContractTranscoder.cs" />
<Compile Include="Memcached\Transcoders\DefaultTranscoder.cs">
</Compile>
Expand Down
36 changes: 36 additions & 0 deletions Enyim.Caching/Memcached/Transcoders/CompressingTranscoder.cs
Original file line number Diff line number Diff line change
@@ -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<byte> 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<byte> 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<byte>(ms.GetBuffer(), 0, (int) ms.Length);
}
}
}
}