Skip to content

Commit 32ab650

Browse files
authored
Merge pull request #117 from cnblogs/add-touch-command
Implement TouchAsync to support "touch" command
2 parents f5a513a + 8dc88f4 commit 32ab650

13 files changed

+341
-253
lines changed

Enyim.Caching.Tests/MemcachedClientMutateTests.cs

+35-21
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,48 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Threading.Tasks;
56
using Xunit;
67

78
namespace Enyim.Caching.Tests
89
{
9-
public class MemcachedClientMutateTests : MemcachedClientTestsBase
10-
{
11-
[Fact]
12-
public void When_Incrementing_Value_Result_Is_Successful()
13-
{
14-
var key = GetUniqueKey("mutate");
15-
var mutateResult = _client.ExecuteIncrement(key, 100, 10);
16-
MutateAssertPass(mutateResult, 100);
10+
public class MemcachedClientMutateTests : MemcachedClientTestsBase
11+
{
12+
[Fact]
13+
public void When_Incrementing_Value_Result_Is_Successful()
14+
{
15+
var key = GetUniqueKey("mutate");
16+
var mutateResult = _client.ExecuteIncrement(key, 100, 10);
17+
MutateAssertPass(mutateResult, 100);
1718

18-
mutateResult = _client.ExecuteIncrement(key, 100, 10);
19-
MutateAssertPass(mutateResult, 110);
20-
}
19+
mutateResult = _client.ExecuteIncrement(key, 100, 10);
20+
MutateAssertPass(mutateResult, 110);
21+
}
2122

22-
[Fact]
23-
public void When_Decrementing_Value_Result_Is_Successful()
24-
{
25-
var key = GetUniqueKey("mutate");
26-
var mutateResult = _client.ExecuteDecrement(key, 100, 10);
27-
MutateAssertPass(mutateResult, 100);
23+
[Fact]
24+
public void When_Decrementing_Value_Result_Is_Successful()
25+
{
26+
var key = GetUniqueKey("mutate");
27+
var mutateResult = _client.ExecuteDecrement(key, 100, 10);
28+
MutateAssertPass(mutateResult, 100);
2829

29-
mutateResult = _client.ExecuteDecrement(key, 100, 10);
30-
MutateAssertPass(mutateResult, 90);
31-
}
32-
}
30+
mutateResult = _client.ExecuteDecrement(key, 100, 10);
31+
MutateAssertPass(mutateResult, 90);
32+
}
33+
34+
[Fact]
35+
public async Task When_Touch_Item_Result_Is_Successful()
36+
{
37+
var key = GetUniqueKey("touch");
38+
await _client.AddAsync(key, "value", 1);
39+
Assert.True((await _client.GetAsync<string>(key)).Success);
40+
var result = await _client.TouchAsync(key, TimeSpan.FromSeconds(60));
41+
await Task.Delay(1010);
42+
Assert.True(result.Success, "Success was false");
43+
Assert.True((result.StatusCode ?? 0) == 0, "StatusCode was not null or 0");
44+
Assert.True((await _client.GetAsync<string>(key)).Success);
45+
}
46+
}
3347
}
3448

3549
#region [ License information ]

Enyim.Caching/IMemcachedClient.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IMemcachedClient : IDisposable
1313

1414
bool Set(string key, object value, int cacheSeconds);
1515
Task<bool> SetAsync(string key, object value, int cacheSeconds);
16-
16+
1717
bool Replace(string key, object value, int cacheSeconds);
1818
Task<bool> ReplaceAsync(string key, object value, int cacheSeconds);
1919

@@ -66,6 +66,9 @@ public interface IMemcachedClient : IDisposable
6666
CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas);
6767
CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas);
6868

69+
Task<IOperationResult> TouchAsync(string key, DateTime expiresAt);
70+
Task<IOperationResult> TouchAsync(string key, TimeSpan validFor);
71+
6972
bool Remove(string key);
7073
Task<bool> RemoveAsync(string key);
7174

Enyim.Caching/Memcached/Enums.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
using Enyim.Caching.Memcached.Protocol.Binary;
12
using System;
23

34
namespace Enyim.Caching.Memcached
45
{
5-
public enum MutationMode : byte { Increment = 0x05, Decrement = 0x06 };
6-
public enum ConcatenationMode : byte { Append = 0x0E, Prepend = 0x0F };
7-
public enum MemcachedProtocol { Binary, Text }
6+
public enum MutationMode : byte { Increment = 0x05, Decrement = 0x06, Touch = OpCode.Touch };
7+
public enum ConcatenationMode : byte { Append = 0x0E, Prepend = 0x0F };
8+
public enum MemcachedProtocol { Binary, Text }
89
}
910

1011
#region [ License information ]
1112
/* ************************************************************
1213
*
13-
* Copyright (c) 2010 Attila Kiskó, enyim.com
14+
* Copyright (c) 2010 Attila Kisk? enyim.com
1415
*
1516
* Licensed under the Apache License, Version 2.0 (the "License");
1617
* you may not use this file except in compliance with the License.

Enyim.Caching/Memcached/IOperationFactory.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55

66
namespace Enyim.Caching.Memcached
77
{
8-
public interface IOperationFactory
9-
{
10-
IGetOperation Get(string key);
11-
IMultiGetOperation MultiGet(IList<string> keys);
8+
public interface IOperationFactory
9+
{
10+
IGetOperation Get(string key);
11+
IMultiGetOperation MultiGet(IList<string> keys);
1212

13-
IStoreOperation Store(StoreMode mode, string key, CacheItem value, uint expires, ulong cas);
14-
IDeleteOperation Delete(string key, ulong cas);
15-
IMutatorOperation Mutate(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas);
16-
IConcatOperation Concat(ConcatenationMode mode, string key, ulong cas, ArraySegment<byte> data);
13+
IStoreOperation Store(StoreMode mode, string key, CacheItem value, uint expires, ulong cas);
14+
IDeleteOperation Delete(string key, ulong cas);
15+
IMutatorOperation Mutate(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas);
16+
IConcatOperation Concat(ConcatenationMode mode, string key, ulong cas, ArraySegment<byte> data);
1717

18-
IStatsOperation Stats(string type);
19-
IFlushOperation Flush();
20-
}
18+
IStatsOperation Stats(string type);
19+
IFlushOperation Flush();
20+
}
2121
}
2222

2323
#region [ License information ]

Enyim.Caching/Memcached/MemcachedNode.cs

+1
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
863863
}
864864
else
865865
{
866+
_logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}");
866867
readResult.Combine(result);
867868
}
868869
return result;

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

+33-33
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,49 @@
55

66
namespace Enyim.Caching.Memcached.Protocol.Binary
77
{
8-
/// <summary>
9-
/// Implements append/prepend.
10-
/// </summary>
11-
public class ConcatOperation : BinarySingleItemOperation, IConcatOperation
12-
{
13-
private ArraySegment<byte> data;
14-
private ConcatenationMode mode;
8+
/// <summary>
9+
/// Implements append/prepend.
10+
/// </summary>
11+
public class ConcatOperation : BinarySingleItemOperation, IConcatOperation
12+
{
13+
private readonly ArraySegment<byte> data;
14+
private readonly ConcatenationMode mode;
1515

16-
public ConcatOperation(ConcatenationMode mode, string key, ArraySegment<byte> data)
17-
: base(key)
18-
{
19-
this.data = data;
20-
this.mode = mode;
21-
}
16+
public ConcatOperation(ConcatenationMode mode, string key, ArraySegment<byte> data)
17+
: base(key)
18+
{
19+
this.data = data;
20+
this.mode = mode;
21+
}
2222

23-
protected override BinaryRequest Build()
24-
{
25-
var request = new BinaryRequest((OpCode)this.mode)
26-
{
27-
Key = this.Key,
28-
Cas = this.Cas,
29-
Data = this.data
30-
};
23+
protected override BinaryRequest Build()
24+
{
25+
var request = new BinaryRequest((OpCode)this.mode)
26+
{
27+
Key = this.Key,
28+
Cas = this.Cas,
29+
Data = this.data
30+
};
3131

32-
return request;
33-
}
32+
return request;
33+
}
3434

35-
protected override IOperationResult ProcessResponse(BinaryResponse response)
36-
{
37-
return new BinaryOperationResult() { Success = true };
38-
}
35+
protected override IOperationResult ProcessResponse(BinaryResponse response)
36+
{
37+
return new BinaryOperationResult() { Success = true };
38+
}
3939

40-
ConcatenationMode IConcatOperation.Mode
41-
{
42-
get { return this.mode; }
43-
}
44-
}
40+
ConcatenationMode IConcatOperation.Mode
41+
{
42+
get { return this.mode; }
43+
}
44+
}
4545
}
4646

4747
#region [ License information ]
4848
/* ************************************************************
4949
*
50-
* Copyright (c) 2010 Attila Kiskó, enyim.com
50+
* Copyright (c) 2010 Attila Kisk? enyim.com
5151
*
5252
* Licensed under the Apache License, Version 2.0 (the "License");
5353
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)