Skip to content

Commit 75baff2

Browse files
authored
Merge pull request #212 from cnblogs/improve-getwithcas
refactor: call TryGetWithCas<T> instead
2 parents d8cf184 + c19731e commit 75baff2

File tree

4 files changed

+43
-74
lines changed

4 files changed

+43
-74
lines changed

src/Enyim.Caching/Memcached/Transcoders/DefaultTranscoder.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
2-
using System.Linq;
3-
using System.IO;
4-
using System.Text;
5-
using System.Runtime.Serialization;
1+
using Newtonsoft.Json;
62
using Newtonsoft.Json.Bson;
3+
using System;
74
using System.Collections;
5+
using System.IO;
6+
using System.Linq;
87
using System.Reflection;
9-
using Newtonsoft.Json;
8+
using System.Text;
109

1110
namespace Enyim.Caching.Memcached
1211
{
@@ -16,7 +15,7 @@ namespace Enyim.Caching.Memcached
1615
public class DefaultTranscoder : ITranscoder
1716
{
1817
public const uint RawDataFlag = 0xfa52;
19-
private static readonly ArraySegment<byte> NullArray = new ArraySegment<byte>(new byte[0]);
18+
private static readonly ArraySegment<byte> NullArray = new([]);
2019

2120
CacheItem ITranscoder.Serialize(object value)
2221
{
@@ -46,22 +45,18 @@ public virtual T Deserialize<T>(CacheItem item)
4645
}
4746
else
4847
{
49-
return default(T);
48+
return default;
5049
}
5150
}
5251

53-
using (var ms = new MemoryStream(item.Data.ToArray()))
52+
using var ms = new MemoryStream(item.Data.ToArray());
53+
using var reader = new BsonDataReader(ms);
54+
if (typeof(T).GetTypeInfo().ImplementedInterfaces.Contains(typeof(IEnumerable)))
5455
{
55-
using (var reader = new BsonDataReader(ms))
56-
{
57-
if (typeof(T).GetTypeInfo().ImplementedInterfaces.Contains(typeof(IEnumerable)))
58-
{
59-
reader.ReadRootValueAsArray = true;
60-
}
61-
var serializer = new JsonSerializer();
62-
return serializer.Deserialize<T>(reader);
63-
}
56+
reader.ReadRootValueAsArray = true;
6457
}
58+
var serializer = new JsonSerializer();
59+
return serializer.Deserialize<T>(reader);
6560
}
6661

6762
protected virtual CacheItem Serialize(object value)

src/Enyim.Caching/MemcachedClient.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,19 +423,14 @@ public CasResult<object> GetWithCas(string key)
423423

424424
public CasResult<T> GetWithCas<T>(string key)
425425
{
426-
CasResult<T> tmp;
427-
428-
return TryGetWithCas(key, out tmp)
429-
? new CasResult<T> { Cas = tmp.Cas, Result = tmp.Result }
430-
: new CasResult<T> { Cas = tmp.Cas, Result = default };
426+
return TryGetWithCas<T>(key, out var value)
427+
? value
428+
: new CasResult<T> { Cas = value.Cas, Result = default };
431429
}
432430

433431
public bool TryGetWithCas(string key, out CasResult<object> value)
434432
{
435-
object tmp;
436-
ulong cas;
437-
438-
var retval = PerformTryGet(key, out cas, out tmp);
433+
var retval = PerformTryGet(key, out ulong cas, out object tmp);
439434

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

test/MemcachedTest/BinaryMemcachedClientTest.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Enyim.Caching;
22
using Enyim.Caching.Memcached;
3+
using System.Collections.Generic;
34
using System.Threading.Tasks;
45
using Xunit;
56

@@ -49,31 +50,31 @@ public async Task IncrementNoDefaultTest()
4950
[Fact]
5051
public virtual void CASTest()
5152
{
52-
using (MemcachedClient client = GetClient())
53-
{
54-
// store the item
55-
var r1 = client.Cas(StoreMode.Set, "CasItem1", "foo");
56-
57-
Assert.True(r1.Result, "Initial set failed.");
58-
Assert.NotEqual(r1.Cas, (ulong)0);
53+
using MemcachedClient client = GetClient();
54+
var value = new List<string> { "foo" };
5955

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

63-
Assert.Equal("foo", r2.Result);
64-
Assert.Equal(r1.Cas, r2.Cas);
59+
Assert.True(r1.Result, "Initial set failed.");
60+
Assert.NotEqual((ulong)0, r1.Cas);
6561

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

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

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

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

74-
var r5 = client.GetWithCas<string>("CasItem1");
75-
Assert.Equal("baz", r5.Result);
76-
}
76+
var r5 = client.GetWithCas<List<string>>("CasItem1");
77+
Assert.Equal("baz", r5.Result[0]);
7778
}
7879

7980
[Fact]

test/MemcachedTest/MemcachedClientTest.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using System;
21
using Enyim.Caching;
32
using Enyim.Caching.Memcached;
4-
using Enyim.Caching.Memcached.Transcoders;
5-
using System.Collections.Generic;
6-
using System.Text;
7-
using Xunit;
83
using Microsoft.Extensions.DependencyInjection;
94
using Microsoft.Extensions.Logging;
10-
using System.Threading.Tasks;
5+
using System;
6+
using System.Collections.Generic;
117
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
1211

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

20-
protected virtual MemcachedClient GetClient(MemcachedProtocol protocol = MemcachedProtocol.Binary, bool useBinaryFormatterTranscoder = false)
19+
protected virtual MemcachedClient GetClient(MemcachedProtocol protocol = MemcachedProtocol.Binary)
2120
{
2221
IServiceCollection services = new ServiceCollection();
2322
services.AddEnyimMemcached(options =>
2423
{
2524
options.AddServer("memcached", 11211);
2625
options.Protocol = protocol;
27-
options.Transcoder = "MessagePackTranscoder";
26+
// options.Transcoder = "MessagePackTranscoder";
2827
});
29-
if (useBinaryFormatterTranscoder)
30-
{
31-
services.AddSingleton<ITranscoder, BinaryFormatterTranscoder>();
32-
}
3328

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

@@ -66,11 +61,6 @@ public async Task StoreObjectTest()
6661
{
6762
Assert.True(await client.StoreAsync(StoreMode.Set, TestObjectKey, td, DateTime.Now.AddSeconds(5)));
6863
}
69-
70-
using (MemcachedClient client = GetClient(MemcachedProtocol.Binary, true))
71-
{
72-
Assert.True(await client.StoreAsync(StoreMode.Set, TestObjectKey, td, DateTime.Now.AddSeconds(5)));
73-
}
7464
}
7565

7666
[Fact]
@@ -94,18 +84,6 @@ public void GetObjectTest()
9484
Assert.Equal(19810619, td2.FieldC);
9585
Assert.True(td2.FieldD, "Object was corrupted.");
9686
}
97-
98-
using (MemcachedClient client = GetClient(MemcachedProtocol.Binary, true))
99-
{
100-
Assert.True(client.Store(StoreMode.Set, TestObjectKey, td), "Initialization failed.");
101-
TestData td2 = client.Get<TestData>(TestObjectKey);
102-
103-
Assert.NotNull(td2);
104-
Assert.Equal("Hello", td2.FieldA);
105-
Assert.Equal("World", td2.FieldB);
106-
Assert.Equal(19810619, td2.FieldC);
107-
Assert.True(td2.FieldD, "Object was corrupted.");
108-
}
10987
}
11088

11189
[Fact]

0 commit comments

Comments
 (0)