Skip to content
Merged
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
19 changes: 17 additions & 2 deletions Enyim.Caching/Configuration/MemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,24 @@ public MemcachedClientConfiguration(
NodeLocator = options.Servers.Count > 1 ? typeof(DefaultNodeLocator) : typeof(SingleNodeLocator);
}

if(options.Transcoder != null)
if(!string.IsNullOrEmpty(options.Transcoder))
{
_transcoder = options.Transcoder;
try
{
if (options.Transcoder == "BinaryFormatterTranscoder")
options.Transcoder = "Enyim.Caching.Memcached.Transcoders.BinaryFormatterTranscoder";

var transcoderType = Type.GetType(options.Transcoder);
if (transcoderType != null)
{
Transcoder = Activator.CreateInstance(transcoderType) as ITranscoder;
_logger.LogDebug($"Use '{options.Transcoder}'");
}
}
catch (Exception ex)
{
_logger.LogError(new EventId(), ex, $"Unable to load '{options.Transcoder}'");
}
}

if (options.NodeLocatorFactory != null)
Expand Down
2 changes: 1 addition & 1 deletion Enyim.Caching/Configuration/MemcachedClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MemcachedClientOptions : IOptions<MemcachedClientOptions>

public string KeyTransformer { get; set; }

public ITranscoder Transcoder { get; set; }
public string Transcoder { get; set; }

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

Expand Down
33 changes: 33 additions & 0 deletions Enyim.Caching/Memcached/Transcoders/BinaryFormatterTranscoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Enyim.Caching.Memcached.Transcoders
{
public class BinaryFormatterTranscoder : DefaultTranscoder
{
protected override ArraySegment<byte> SerializeObject(object value)
{
using (var ms = new MemoryStream())
{
new BinaryFormatter().Serialize(ms, value);
return new ArraySegment<byte>(ms.GetBuffer(), 0, (int)ms.Length);
}
}

public override T Deserialize<T>(CacheItem item)
{
return (T)base.Deserialize(item);
}

protected override object DeserializeObject(ArraySegment<byte> value)
{
using (var ms = new MemoryStream(value.Array, value.Offset, value.Count))
{
return new BinaryFormatter().Deserialize(ms);
}
}
}
}
4 changes: 2 additions & 2 deletions Enyim.Caching/Memcached/Transcoders/DefaultTranscoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object ITranscoder.Deserialize(CacheItem item)
return this.Deserialize(item);
}

T ITranscoder.Deserialize<T>(CacheItem item)
public virtual T Deserialize<T>(CacheItem item)
{
if (item.Data == null || item.Data.Count == 0) return default(T);

Expand Down Expand Up @@ -184,7 +184,7 @@ protected virtual object Deserialize(CacheItem item)
// earlier versions serialized decimals with TypeCode.Decimal
// even though they were saved by BinaryFormatter
case TypeCode.Decimal:
//case TypeCode.Object: return this.DeserializeObject(data);
case TypeCode.Object: return this.DeserializeObject(data);
default: throw new InvalidOperationException("Unknown TypeCode was returned: " + code);
}
}
Expand Down
27 changes: 25 additions & 2 deletions MemcachedTest/MemcachedClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ public abstract class MemcachedClientTest
private static readonly Enyim.Caching.ILog log = Enyim.Caching.LogManager.GetLogger(typeof(MemcachedClientTest));
public const string TestObjectKey = "Hello_World";

protected virtual MemcachedClient GetClient(MemcachedProtocol protocol = MemcachedProtocol.Binary)
protected virtual MemcachedClient GetClient(MemcachedProtocol protocol = MemcachedProtocol.Binary, bool useBinaryFormatterTranscoder = false)
{
IServiceCollection services = new ServiceCollection();
services.AddEnyimMemcached(options =>
{
options.AddServer("memcached", 11211);
options.Protocol = protocol;
if (useBinaryFormatterTranscoder)
{
options.Transcoder = "BinaryFormatterTranscoder";
}
});
services.AddLogging();
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Error).AddConsole());

IServiceProvider serviceProvider = services.BuildServiceProvider();
var client = serviceProvider.GetService<IMemcachedClient>() as MemcachedClient;
client.Remove("VALUE");
return client;
}

[Serializable]// This attribute is for BinaryFormatterTranscoder
public class TestData
{
public TestData() { }
Expand All @@ -60,6 +66,11 @@ public async Task StoreObjectTest()
{
Assert.True(await client.StoreAsync(StoreMode.Set, TestObjectKey, td, DateTime.Now.AddSeconds(5)));
}

using (MemcachedClient client = GetClient(MemcachedProtocol.Binary, true))
{
Assert.True(await client.StoreAsync(StoreMode.Set, TestObjectKey, td, DateTime.Now.AddSeconds(5)));
}
}

[Fact]
Expand All @@ -83,6 +94,18 @@ public void GetObjectTest()
Assert.Equal(td2.FieldC, 19810619);
Assert.True(td2.FieldD, "Object was corrupted.");
}

using (MemcachedClient client = GetClient(MemcachedProtocol.Binary, true))
{
Assert.True(client.Store(StoreMode.Set, TestObjectKey, td), "Initialization failed.");
TestData td2 = client.Get<TestData>(TestObjectKey);

Assert.NotNull(td2);
Assert.Equal(td2.FieldA, "Hello");
Assert.Equal(td2.FieldB, "World");
Assert.Equal(td2.FieldC, 19810619);
Assert.True(td2.FieldD, "Object was corrupted.");
}
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions MemcachedTest/MemcachedTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.0-beta3-build3705" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta3-build3705" />
Expand Down
3 changes: 2 additions & 1 deletion SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"receiveTimeout": "00:00:15",
"deadTimeout": "00:00:15",
"queueTimeout": "00:00:00.150"
}
}//,
//"Transcoder": "BinaryFormatterTranscoder"
//,
//"KeyTransformer": "Enyim.Caching.Memcached.SHA1KeyTransformer"
//,
Expand Down