Skip to content

Commit

Permalink
Merge pull request #212 from cnblogs/improve-getwithcas
Browse files Browse the repository at this point in the history
refactor: call TryGetWithCas<T> instead
  • Loading branch information
cnblogs-dudu authored Feb 14, 2024
2 parents d8cf184 + c19731e commit 75baff2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 74 deletions.
31 changes: 13 additions & 18 deletions src/Enyim.Caching/Memcached/Transcoders/DefaultTranscoder.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using System.Text;

namespace Enyim.Caching.Memcached
{
Expand All @@ -16,7 +15,7 @@ namespace Enyim.Caching.Memcached
public class DefaultTranscoder : ITranscoder
{
public const uint RawDataFlag = 0xfa52;
private static readonly ArraySegment<byte> NullArray = new ArraySegment<byte>(new byte[0]);
private static readonly ArraySegment<byte> NullArray = new([]);

CacheItem ITranscoder.Serialize(object value)
{
Expand Down Expand Up @@ -46,22 +45,18 @@ public virtual T Deserialize<T>(CacheItem item)
}
else
{
return default(T);
return default;
}
}

using (var ms = new MemoryStream(item.Data.ToArray()))
using var ms = new MemoryStream(item.Data.ToArray());
using var reader = new BsonDataReader(ms);
if (typeof(T).GetTypeInfo().ImplementedInterfaces.Contains(typeof(IEnumerable)))
{
using (var reader = new BsonDataReader(ms))
{
if (typeof(T).GetTypeInfo().ImplementedInterfaces.Contains(typeof(IEnumerable)))
{
reader.ReadRootValueAsArray = true;
}
var serializer = new JsonSerializer();
return serializer.Deserialize<T>(reader);
}
reader.ReadRootValueAsArray = true;
}
var serializer = new JsonSerializer();
return serializer.Deserialize<T>(reader);
}

protected virtual CacheItem Serialize(object value)
Expand Down
13 changes: 4 additions & 9 deletions src/Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,19 +423,14 @@ public CasResult<object> GetWithCas(string key)

public CasResult<T> GetWithCas<T>(string key)
{
CasResult<T> tmp;

return TryGetWithCas(key, out tmp)
? new CasResult<T> { Cas = tmp.Cas, Result = tmp.Result }
: new CasResult<T> { Cas = tmp.Cas, Result = default };
return TryGetWithCas<T>(key, out var value)
? value
: new CasResult<T> { Cas = value.Cas, Result = default };
}

public bool TryGetWithCas(string key, out CasResult<object> value)
{
object tmp;
ulong cas;

var retval = PerformTryGet(key, out cas, out tmp);
var retval = PerformTryGet(key, out ulong cas, out object tmp);

value = new CasResult<object> { Cas = cas, Result = tmp };

Expand Down
37 changes: 19 additions & 18 deletions test/MemcachedTest/BinaryMemcachedClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Enyim.Caching;
using Enyim.Caching.Memcached;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -49,31 +50,31 @@ public async Task IncrementNoDefaultTest()
[Fact]
public virtual void CASTest()
{
using (MemcachedClient client = GetClient())
{
// store the item
var r1 = client.Cas(StoreMode.Set, "CasItem1", "foo");

Assert.True(r1.Result, "Initial set failed.");
Assert.NotEqual(r1.Cas, (ulong)0);
using MemcachedClient client = GetClient();
var value = new List<string> { "foo" };

// get back the item and check the cas value (it should match the cas from the set)
var r2 = client.GetWithCas<string>("CasItem1");
// store the item
var r1 = client.Cas(StoreMode.Set, "CasItem1", value);

Assert.Equal("foo", r2.Result);
Assert.Equal(r1.Cas, r2.Cas);
Assert.True(r1.Result, "Initial set failed.");
Assert.NotEqual((ulong)0, r1.Cas);

var r3 = client.Cas(StoreMode.Set, "CasItem1", "bar", r1.Cas - 1);
// get back the item and check the cas value (it should match the cas from the set)
var r2 = client.GetWithCas<List<string>>("CasItem1");

Assert.False(r3.Result, "Overwriting with 'bar' should have failed.");
Assert.Equal("foo", r2.Result[0]);
Assert.Equal(r1.Cas, r2.Cas);

var r4 = client.Cas(StoreMode.Set, "CasItem1", "baz", r2.Cas);
value[0] = "bar";
var r3 = client.Cas(StoreMode.Set, "CasItem1", value, r1.Cas - 1);
Assert.False(r3.Result, "Overwriting with 'bar' should have failed.");

Assert.True(r4.Result, "Overwriting with 'baz' should have succeeded.");
value[0] = "baz";
var r4 = client.Cas(StoreMode.Set, "CasItem1", value, r2.Cas);
Assert.True(r4.Result, "Overwriting with 'baz' should have succeeded.");

var r5 = client.GetWithCas<string>("CasItem1");
Assert.Equal("baz", r5.Result);
}
var r5 = client.GetWithCas<List<string>>("CasItem1");
Assert.Equal("baz", r5.Result[0]);
}

[Fact]
Expand Down
36 changes: 7 additions & 29 deletions test/MemcachedTest/MemcachedClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Transcoders;
using System.Collections.Generic;
using System.Text;
using Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace MemcachedTest
{
Expand All @@ -17,19 +16,15 @@ public abstract class MemcachedClientTest
private static readonly ILog _log = LogManager.GetLogger(typeof(MemcachedClientTest));
public const string TestObjectKey = "Hello_World";

protected virtual MemcachedClient GetClient(MemcachedProtocol protocol = MemcachedProtocol.Binary, bool useBinaryFormatterTranscoder = false)
protected virtual MemcachedClient GetClient(MemcachedProtocol protocol = MemcachedProtocol.Binary)
{
IServiceCollection services = new ServiceCollection();
services.AddEnyimMemcached(options =>
{
options.AddServer("memcached", 11211);
options.Protocol = protocol;
options.Transcoder = "MessagePackTranscoder";
// options.Transcoder = "MessagePackTranscoder";
});
if (useBinaryFormatterTranscoder)
{
services.AddSingleton<ITranscoder, BinaryFormatterTranscoder>();
}

services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Warning).AddConsole());

Expand Down Expand Up @@ -66,11 +61,6 @@ 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 @@ -94,18 +84,6 @@ public void GetObjectTest()
Assert.Equal(19810619, td2.FieldC);
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("Hello", td2.FieldA);
Assert.Equal("World", td2.FieldB);
Assert.Equal(19810619, td2.FieldC);
Assert.True(td2.FieldD, "Object was corrupted.");
}
}

[Fact]
Expand Down

0 comments on commit 75baff2

Please sign in to comment.