Skip to content

Commit 9aae92d

Browse files
authored
Merge pull request #74 from cnblogs/tweak-and-revert
Tweak and revert
2 parents fbbc614 + 19cafc7 commit 9aae92d

File tree

8 files changed

+210
-140
lines changed

8 files changed

+210
-140
lines changed

Enyim.Caching.Tests/MemcachedClientGetTests.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,35 @@ public void When_Getting_SByte_Result_Is_Successful()
102102
public async Task GetValueOrCreateAsyncTest()
103103
{
104104
var key = "GetValueOrCreateAsyncTest_" + Guid.NewGuid();
105-
await _client.GetValueOrCreateAsync(key, 10, () => GenerateValue());
106-
var cacheValue = await _client.GetValueAsync<string>(key);
107-
Assert.Equal(nameof(GetValueOrCreateAsyncTest), cacheValue);
105+
var posts1 = await _client.GetValueOrCreateAsync(
106+
key,
107+
10,
108+
async () => await GenerateValue());
109+
Assert.NotNull(posts1);
110+
111+
var posts2 = await _client.GetValueAsync<IEnumerable<BlogPost>>(key);
112+
Assert.NotNull(posts2);
113+
114+
Assert.Equal(posts1.First().Title, posts2.First().Title);
108115
}
109116

110-
private Task<string> GenerateValue()
117+
private Task<IEnumerable<BlogPost>> GenerateValue()
111118
{
112-
return Task.FromResult(nameof(GetValueOrCreateAsyncTest));
119+
var posts = new List<BlogPost>()
120+
{
121+
new BlogPost{ Title = "test title 1", Body = "test body 1" },
122+
new BlogPost{ Title = "test title 2", Body = "test body 2" }
123+
};
124+
125+
return Task.FromResult(posts.AsEnumerable());
113126
}
114127
}
128+
129+
internal class BlogPost
130+
{
131+
public string Title { get; set; }
132+
public string Body { get; set; }
133+
}
115134
}
116135

117136
#region [ License information ]

Enyim.Caching.Tests/MemcachedClientTestsBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ protected string GetUniqueKey(string prefix = null)
3939

4040
protected IEnumerable<string> GetUniqueKeys(string prefix = null, int max = 5)
4141
{
42-
4342
var keys = new List<string>(max);
4443
for (int i = 0; i < max; i++)
4544
{

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
using Enyim.Caching.Configuration;
2+
using Enyim.Caching.Memcached.Protocol.Binary;
3+
using Enyim.Caching.Memcached.Results;
4+
using Enyim.Caching.Memcached.Results.Extensions;
5+
using Enyim.Collections;
6+
using Microsoft.Extensions.Logging;
17
using System;
28
using System.Collections.Generic;
39
using System.Diagnostics;
@@ -7,12 +13,6 @@
713
using System.Security;
814
using System.Threading;
915
using System.Threading.Tasks;
10-
using Enyim.Caching.Configuration;
11-
using Enyim.Caching.Memcached.Protocol.Binary;
12-
using Enyim.Caching.Memcached.Results;
13-
using Enyim.Caching.Memcached.Results.Extensions;
14-
using Enyim.Collections;
15-
using Microsoft.Extensions.Logging;
1616

1717
namespace Enyim.Caching.Memcached
1818
{

Enyim.Caching/Memcached/Protocol/Binary/BinaryConverter.cs

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,110 @@ namespace Enyim.Caching.Memcached.Protocol.Binary
55
{
66
public static class BinaryConverter
77
{
8-
public static ushort DecodeUInt16(Span<byte> buffer, int offset)
8+
public static unsafe ushort DecodeUInt16(byte[] buffer, int offset)
99
{
1010
return (ushort)((buffer[offset] << 8) + buffer[offset + 1]);
1111
}
1212

13-
public static int DecodeInt32(Span<byte> buffer, int offset)
13+
public static unsafe ushort DecodeUInt16(byte* buffer, int offset)
1414
{
15-
var slice = buffer.Slice(offset);
15+
return (ushort)((buffer[offset] << 8) + buffer[offset + 1]);
16+
}
17+
18+
public static unsafe int DecodeInt32(ArraySegment<byte> segment, int offset)
19+
{
20+
fixed (byte* buffer = segment.Array)
21+
{
22+
byte* ptr = buffer + segment.Offset + offset;
23+
24+
return DecodeInt32(buffer, 0);
25+
}
26+
}
27+
28+
public static unsafe int DecodeInt32(byte* buffer, int offset)
29+
{
30+
buffer += offset;
31+
32+
return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
33+
}
34+
35+
public static unsafe int DecodeInt32(byte[] buffer, int offset)
36+
{
37+
return (buffer[offset] << 24) | (buffer[offset + 1] << 16) | (buffer[offset + 2] << 8) | buffer[offset + 3];
38+
}
1639

17-
return (slice[0] << 24) | (slice[1] << 16) | (slice[2] << 8) | slice[3];
40+
public static unsafe ulong DecodeUInt64(byte[] buffer, int offset)
41+
{
42+
fixed (byte* ptr = buffer)
43+
{
44+
return DecodeUInt64(ptr, offset);
45+
}
1846
}
1947

20-
public static unsafe ulong DecodeUInt64(Span<byte> buffer, int offset)
48+
public static unsafe ulong DecodeUInt64(byte* buffer, int offset)
2149
{
22-
var slice = buffer.Slice(offset);
50+
buffer += offset;
2351

24-
var part1 = (uint)((slice[0] << 24) | (slice[1] << 16) | (slice[2] << 8) | slice[3]);
25-
var part2 = (uint)((slice[4] << 24) | (slice[5] << 16) | (slice[6] << 8) | slice[7]);
52+
var part1 = (uint)((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
53+
var part2 = (uint)((buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]);
2654

2755
return ((ulong)part1 << 32) | part2;
2856
}
2957

30-
public static unsafe void EncodeUInt16(uint value, Span<byte> buffer, int offset)
58+
public static unsafe void EncodeUInt16(uint value, byte[] buffer, int offset)
59+
{
60+
fixed (byte* bufferPtr = buffer)
61+
{
62+
EncodeUInt16(value, bufferPtr, offset);
63+
}
64+
}
65+
66+
public static unsafe void EncodeUInt16(uint value, byte* buffer, int offset)
3167
{
32-
var slice = buffer.Slice(offset);
68+
byte* ptr = buffer + offset;
3369

34-
slice[0] = (byte)(value >> 8);
35-
slice[1] = (byte)(value & 255);
70+
ptr[0] = (byte)(value >> 8);
71+
ptr[1] = (byte)(value & 255);
3672
}
3773

38-
public static unsafe void EncodeUInt32(uint value, Span<byte> buffer, int offset)
74+
public static unsafe void EncodeUInt32(uint value, byte[] buffer, int offset)
3975
{
40-
var slice = buffer.Slice(offset);
76+
fixed (byte* bufferPtr = buffer)
77+
{
78+
EncodeUInt32(value, bufferPtr, offset);
79+
}
80+
}
81+
82+
public static unsafe void EncodeUInt32(uint value, byte* buffer, int offset)
83+
{
84+
byte* ptr = buffer + offset;
4185

42-
slice[0] = (byte)(value >> 24);
43-
slice[1] = (byte)(value >> 16);
44-
slice[2] = (byte)(value >> 8);
45-
slice[3] = (byte)(value & 255);
86+
ptr[0] = (byte)(value >> 24);
87+
ptr[1] = (byte)(value >> 16);
88+
ptr[2] = (byte)(value >> 8);
89+
ptr[3] = (byte)(value & 255);
4690
}
4791

48-
public static unsafe void EncodeUInt64(ulong value, Span<byte> buffer, int offset)
92+
public static unsafe void EncodeUInt64(ulong value, byte[] buffer, int offset)
4993
{
50-
var slice = buffer.Slice(offset);
94+
fixed (byte* bufferPtr = buffer)
95+
{
96+
EncodeUInt64(value, bufferPtr, offset);
97+
}
98+
}
5199

52-
slice[0] = (byte)(value >> 56);
53-
slice[1] = (byte)(value >> 48);
54-
slice[2] = (byte)(value >> 40);
55-
slice[3] = (byte)(value >> 32);
56-
slice[4] = (byte)(value >> 24);
57-
slice[5] = (byte)(value >> 16);
58-
slice[6] = (byte)(value >> 8);
59-
slice[7] = (byte)(value & 255);
100+
public static unsafe void EncodeUInt64(ulong value, byte* buffer, int offset)
101+
{
102+
byte* ptr = buffer + offset;
103+
104+
ptr[0] = (byte)(value >> 56);
105+
ptr[1] = (byte)(value >> 48);
106+
ptr[2] = (byte)(value >> 40);
107+
ptr[3] = (byte)(value >> 32);
108+
ptr[4] = (byte)(value >> 24);
109+
ptr[5] = (byte)(value >> 16);
110+
ptr[6] = (byte)(value >> 8);
111+
ptr[7] = (byte)(value & 255);
60112
}
61113

62114
public static byte[] EncodeKey(string key)

Enyim.Caching/Memcached/Protocol/Binary/BinaryResponse.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Text;
3+
using System.Diagnostics;
44
using System.Threading.Tasks;
55

66
namespace Enyim.Caching.Memcached.Protocol.Binary
@@ -64,7 +64,7 @@ public unsafe bool Read(PooledSocket socket)
6464
if (dataLength > 0)
6565
{
6666
var data = new byte[dataLength];
67-
socket.Read(data, 0, dataLength);
67+
socket.Read(data, 0, dataLength);
6868

6969
this.Extra = new ArraySegment<byte>(data, 0, extraLength);
7070
this.Data = new ArraySegment<byte>(data, extraLength, data.Length - extraLength);
@@ -207,22 +207,24 @@ private void DoDecodeBody(AsyncIOArgs asyncEvent)
207207
if (this.shouldCallNext) this.next(true);
208208
}
209209

210-
211-
private void DeserializeHeader(Span<byte> header, out int dataLength, out int extraLength)
210+
private unsafe void DeserializeHeader(byte[] header, out int dataLength, out int extraLength)
212211
{
213-
if (header[0] != MAGIC_VALUE)
214-
throw new InvalidOperationException("Expected magic value " + MAGIC_VALUE + ", received: " + header[0]);
212+
fixed (byte* buffer = header)
213+
{
214+
if (buffer[0] != MAGIC_VALUE)
215+
throw new InvalidOperationException("Expected magic value " + MAGIC_VALUE + ", received: " + buffer[0]);
215216

216-
this.DataType = header[HEADER_DATATYPE];
217-
this.Opcode = header[HEADER_OPCODE];
218-
this.StatusCode = BinaryConverter.DecodeUInt16(header, HEADER_STATUS);
217+
this.DataType = buffer[HEADER_DATATYPE];
218+
this.Opcode = buffer[HEADER_OPCODE];
219+
this.StatusCode = BinaryConverter.DecodeUInt16(buffer, HEADER_STATUS);
219220

220-
this.KeyLength = BinaryConverter.DecodeUInt16(header, HEADER_KEY);
221-
this.CorrelationId = BinaryConverter.DecodeInt32(header, HEADER_OPAQUE);
222-
this.CAS = BinaryConverter.DecodeUInt64(header, HEADER_CAS);
221+
this.KeyLength = BinaryConverter.DecodeUInt16(buffer, HEADER_KEY);
222+
this.CorrelationId = BinaryConverter.DecodeInt32(buffer, HEADER_OPAQUE);
223+
this.CAS = BinaryConverter.DecodeUInt64(buffer, HEADER_CAS);
223224

224-
dataLength = BinaryConverter.DecodeInt32(header, HEADER_BODY);
225-
extraLength = header[HEADER_EXTRA];
225+
dataLength = BinaryConverter.DecodeInt32(buffer, HEADER_BODY);
226+
extraLength = buffer[HEADER_EXTRA];
227+
}
226228
}
227229

228230
private void LogExecutionTime(string title, DateTime startTime, int thresholdMs)

Enyim.Caching/Memcached/Protocol/Binary/GetOperation.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Text;
43
using Enyim.Caching.Memcached.Results;
@@ -38,7 +37,7 @@ protected override IOperationResult ProcessResponse(BinaryResponse response)
3837

3938
if (status == 0)
4039
{
41-
int flags = BinaryConverter.DecodeInt32(response.Extra.AsSpan(), 0);
40+
int flags = BinaryConverter.DecodeInt32(response.Extra, 0);
4241
this.result = new CacheItem((ushort)flags, response.Data);
4342
this.Cas = response.CAS;
4443

0 commit comments

Comments
 (0)