Skip to content

Commit 26857ac

Browse files
committed
Implement BinaryFormatterTranscoder
1 parent 47e4421 commit 26857ac

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,24 @@ public MemcachedClientConfiguration(
125125
NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
126126
}
127127

128-
if(options.Transcoder != null)
128+
if(!string.IsNullOrEmpty(options.Transcoder))
129129
{
130-
_transcoder = options.Transcoder;
130+
try
131+
{
132+
if (options.Transcoder == "BinaryFormatterTranscoder")
133+
options.Transcoder = "Enyim.Caching.Memcached.Transcoders.BinaryFormatterTranscoder";
134+
135+
var transcoderType = Type.GetType(options.Transcoder);
136+
if (transcoderType != null)
137+
{
138+
Transcoder = Activator.CreateInstance(transcoderType) as ITranscoder;
139+
_logger.LogDebug($"Use '{options.Transcoder}'");
140+
}
141+
}
142+
catch (Exception ex)
143+
{
144+
_logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'");
145+
}
131146
}
132147

133148
if (options.NodeLocatorFactory != null)

Enyim.Caching/Configuration/MemcachedClientOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>
1919

2020
public string KeyTransformer { get; set; }
2121

22-
public ITranscoder Transcoder { get; set; }
22+
public string Transcoder { get; set; }
2323

2424
public IProviderFactory<IMemcachedNodeLocator> NodeLocatorFactory { get; set; }
2525

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
using System.Text;
6+
7+
namespace Enyim.Caching.Memcached.Transcoders
8+
{
9+
public class BinaryFormatterTranscoder : DefaultTranscoder
10+
{
11+
protected override ArraySegment<byte> SerializeObject(object value)
12+
{
13+
using (var ms = new MemoryStream())
14+
{
15+
new BinaryFormatter().Serialize(ms, value);
16+
return new ArraySegment<byte>(ms.GetBuffer(), 0, (int)ms.Length);
17+
}
18+
}
19+
20+
public override T Deserialize<T>(CacheItem item)
21+
{
22+
return (T)base.Deserialize(item);
23+
}
24+
25+
protected override object DeserializeObject(ArraySegment<byte> value)
26+
{
27+
using (var ms = new MemoryStream(value.Array, value.Offset, value.Count))
28+
{
29+
return new BinaryFormatter().Deserialize(ms);
30+
}
31+
}
32+
}
33+
}

Enyim.Caching/Memcached/Transcoders/DefaultTranscoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object ITranscoder.Deserialize(CacheItem item)
2828
return this.Deserialize(item);
2929
}
3030

31-
T ITranscoder.Deserialize<T>(CacheItem item)
31+
public virtual T Deserialize<T>(CacheItem item)
3232
{
3333
if (item.Data == null || item.Data.Count == 0) return default(T);
3434

@@ -184,7 +184,7 @@ protected virtual object Deserialize(CacheItem item)
184184
// earlier versions serialized decimals with TypeCode.Decimal
185185
// even though they were saved by BinaryFormatter
186186
case TypeCode.Decimal:
187-
//case TypeCode.Object: return this.DeserializeObject(data);
187+
case TypeCode.Object: return this.DeserializeObject(data);
188188
default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
189189
}
190190
}

SampleWebApp/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"receiveTimeout": "00:00:15",
1414
"deadTimeout": "00:00:15",
1515
"queueTimeout": "00:00:00.150"
16-
}
16+
}//,
17+
//"Transcoder": "BinaryFormatterTranscoder"
1718
//,
1819
//"KeyTransformer": "Enyim.Caching.Memcached.SHA1KeyTransformer"
1920
//,

0 commit comments

Comments
 (0)