Skip to content

Commit f7900d4

Browse files
authored
Merge pull request #122 from cnblogs/introduce-generic-memcachedclient
Introduce IMemcachedClient<T> and MemcachedClient<T>
2 parents f39eb2c + efba5ab commit f7900d4

File tree

9 files changed

+367
-6
lines changed

9 files changed

+367
-6
lines changed

Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.DependencyInjection.Extensions;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
810
using System;
911

1012
namespace Microsoft.Extensions.DependencyInjection
@@ -49,7 +51,7 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
4951
throw new ArgumentNullException(nameof(configurationSection));
5052
}
5153

52-
if(!configurationSection.Exists())
54+
if (!configurationSection.Exists())
5355
{
5456
throw new ArgumentNullException($"{configurationSection.Key} in appsettings.json");
5557
}
@@ -92,6 +94,27 @@ private static IServiceCollection AddEnyimMemcachedInternal(IServiceCollection s
9294
services.AddSingleton<IDistributedCache>(factory => factory.GetService<MemcachedClient>());
9395

9496
return services;
95-
}
97+
}
98+
99+
public static IServiceCollection AddEnyimMemcached<T>(
100+
this IServiceCollection services,
101+
IConfiguration configuration,
102+
string sectionKey)
103+
{
104+
services.AddOptions();
105+
services.Configure<MemcachedClientOptions>(sectionKey, configuration.GetSection(sectionKey));
106+
services.TryAddSingleton<ITranscoder, DefaultTranscoder>();
107+
services.TryAddSingleton<IMemcachedKeyTransformer, DefaultKeyTransformer>();
108+
109+
services.AddSingleton<IMemcachedClient<T>>(sp =>
110+
{
111+
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
112+
var options = sp.GetRequiredService<IOptionsMonitor<MemcachedClientOptions>>();
113+
var conf = new MemcachedClientConfiguration(loggerFactory, options.Get(sectionKey));
114+
return new MemcachedClient<T>(loggerFactory, conf);
115+
});
116+
117+
return services;
118+
}
96119
}
97120
}

Enyim.Caching/IMemcachedClientT.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Enyim.Caching
6+
{
7+
public interface IMemcachedClient<T> : IMemcachedClient
8+
{
9+
}
10+
}

Enyim.Caching/MemcachedClient.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ public partial class MemcachedClient : IMemcachedClient, IMemcachedResultsClient
4242
protected IMemcachedKeyTransformer KeyTransformer { get { return this.keyTransformer; } }
4343
protected ITranscoder Transcoder { get { return this.transcoder; } }
4444

45-
public MemcachedClient(
46-
ILoggerFactory loggerFactor,
47-
IMemcachedClientConfiguration configuration)
45+
public MemcachedClient(ILoggerFactory loggerFactory, IMemcachedClientConfiguration configuration)
4846
{
49-
_logger = loggerFactor.CreateLogger<MemcachedClient>();
47+
_logger = loggerFactory.CreateLogger<MemcachedClient>();
5048

5149
if (configuration == null)
5250
{

Enyim.Caching/MemcachedClientT.cs

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Enyim.Caching.Configuration;
6+
using Enyim.Caching.Memcached;
7+
using Enyim.Caching.Memcached.Results;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Enyim.Caching
11+
{
12+
public class MemcachedClient<T> : IMemcachedClient<T>
13+
{
14+
private readonly IMemcachedClient _memcachedClient;
15+
16+
public event Action<IMemcachedNode> NodeFailed;
17+
18+
public MemcachedClient(ILoggerFactory loggerFactory, IMemcachedClientConfiguration configuration)
19+
{
20+
_memcachedClient = new MemcachedClient(loggerFactory, configuration);
21+
}
22+
23+
public bool Add(string key, object value, int cacheSeconds)
24+
{
25+
return _memcachedClient.Add(key, value, cacheSeconds);
26+
}
27+
28+
public Task<bool> AddAsync(string key, object value, int cacheSeconds)
29+
{
30+
return _memcachedClient.AddAsync(key, value, cacheSeconds);
31+
}
32+
33+
public bool Append(string key, ArraySegment<byte> data)
34+
{
35+
return _memcachedClient.Append(key, data);
36+
}
37+
38+
public CasResult<bool> Append(string key, ulong cas, ArraySegment<byte> data)
39+
{
40+
return _memcachedClient.Append(key, cas, data);
41+
}
42+
43+
public CasResult<bool> Cas(StoreMode mode, string key, object value)
44+
{
45+
return _memcachedClient.Cas(mode, key, value);
46+
}
47+
48+
public CasResult<bool> Cas(StoreMode mode, string key, object value, ulong cas)
49+
{
50+
return _memcachedClient.Cas(mode, key, value, cas);
51+
}
52+
53+
public CasResult<bool> Cas(StoreMode mode, string key, object value, DateTime expiresAt, ulong cas)
54+
{
55+
return _memcachedClient.Cas(mode, key, value, expiresAt, cas);
56+
}
57+
58+
public CasResult<bool> Cas(StoreMode mode, string key, object value, TimeSpan validFor, ulong cas)
59+
{
60+
return _memcachedClient.Cas(mode, key, value, validFor, cas);
61+
}
62+
63+
public ulong Decrement(string key, ulong defaultValue, ulong delta)
64+
{
65+
return _memcachedClient.Decrement(key, defaultValue, delta);
66+
}
67+
68+
public ulong Decrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt)
69+
{
70+
return _memcachedClient.Decrement(key, defaultValue, delta, expiresAt);
71+
}
72+
73+
public ulong Decrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor)
74+
{
75+
return _memcachedClient.Decrement(key, defaultValue, delta, validFor);
76+
}
77+
78+
public CasResult<ulong> Decrement(string key, ulong defaultValue, ulong delta, ulong cas)
79+
{
80+
return _memcachedClient.Decrement(key, defaultValue, delta, cas);
81+
}
82+
83+
public CasResult<ulong> Decrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas)
84+
{
85+
return _memcachedClient.Decrement(key, defaultValue, delta, expiresAt, cas);
86+
}
87+
88+
public CasResult<ulong> Decrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas)
89+
{
90+
return _memcachedClient.Decrement(key, defaultValue, delta, validFor, cas);
91+
}
92+
93+
public void Dispose()
94+
{
95+
_memcachedClient.Dispose();
96+
}
97+
98+
public void FlushAll()
99+
{
100+
_memcachedClient.FlushAll();
101+
}
102+
103+
public Task FlushAllAsync()
104+
{
105+
return _memcachedClient.FlushAllAsync();
106+
}
107+
108+
public object Get(string key)
109+
{
110+
return _memcachedClient.Get(key);
111+
}
112+
113+
public T1 Get<T1>(string key)
114+
{
115+
return _memcachedClient.Get<T1>(key);
116+
}
117+
118+
public IDictionary<string, T1> Get<T1>(IEnumerable<string> keys)
119+
{
120+
return _memcachedClient.Get<T1>(keys);
121+
}
122+
123+
public Task<IGetOperationResult<T1>> GetAsync<T1>(string key)
124+
{
125+
return _memcachedClient.GetAsync<T1>(key);
126+
}
127+
128+
public Task<IDictionary<string, T1>> GetAsync<T1>(IEnumerable<string> keys)
129+
{
130+
return _memcachedClient.GetAsync<T1>(keys);
131+
}
132+
133+
public Task<T1> GetValueAsync<T1>(string key)
134+
{
135+
return _memcachedClient.GetValueAsync<T1>(key);
136+
}
137+
138+
public Task<T1> GetValueOrCreateAsync<T1>(string key, int cacheSeconds, Func<Task<T1>> generator)
139+
{
140+
return _memcachedClient.GetValueOrCreateAsync<T1>(key, cacheSeconds, generator);
141+
}
142+
143+
public CasResult<object> GetWithCas(string key)
144+
{
145+
return _memcachedClient.GetWithCas(key);
146+
}
147+
148+
public CasResult<T1> GetWithCas<T1>(string key)
149+
{
150+
return _memcachedClient.GetWithCas<T1>(key);
151+
}
152+
153+
public IDictionary<string, CasResult<object>> GetWithCas(IEnumerable<string> keys)
154+
{
155+
return _memcachedClient.GetWithCas(keys);
156+
}
157+
158+
public Task<IDictionary<string, CasResult<object>>> GetWithCasAsync(IEnumerable<string> keys)
159+
{
160+
return _memcachedClient.GetWithCasAsync(keys);
161+
}
162+
163+
public ulong Increment(string key, ulong defaultValue, ulong delta)
164+
{
165+
return _memcachedClient.Increment(key, defaultValue, delta);
166+
}
167+
168+
public ulong Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt)
169+
{
170+
return _memcachedClient.Increment(key, defaultValue, delta, expiresAt);
171+
}
172+
173+
public ulong Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor)
174+
{
175+
return _memcachedClient.Increment(key, defaultValue, delta, validFor);
176+
}
177+
178+
public CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, ulong cas)
179+
{
180+
return _memcachedClient.Increment(key, defaultValue, delta, cas);
181+
}
182+
183+
public CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas)
184+
{
185+
return _memcachedClient.Increment(key, defaultValue, delta, expiresAt, cas);
186+
}
187+
188+
public CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas)
189+
{
190+
return _memcachedClient.Increment(key, defaultValue, delta, validFor, cas);
191+
}
192+
193+
public bool Prepend(string key, ArraySegment<byte> data)
194+
{
195+
return _memcachedClient.Prepend(key, data);
196+
}
197+
198+
public CasResult<bool> Prepend(string key, ulong cas, ArraySegment<byte> data)
199+
{
200+
return _memcachedClient.Prepend(key, cas, data);
201+
}
202+
203+
public bool Remove(string key)
204+
{
205+
return _memcachedClient.Remove(key);
206+
}
207+
208+
public Task<bool> RemoveAsync(string key)
209+
{
210+
return _memcachedClient.RemoveAsync(key);
211+
}
212+
213+
public bool Replace(string key, object value, int cacheSeconds)
214+
{
215+
return _memcachedClient.Replace(key, value, cacheSeconds);
216+
}
217+
218+
public Task<bool> ReplaceAsync(string key, object value, int cacheSeconds)
219+
{
220+
return _memcachedClient.ReplaceAsync(key, value, cacheSeconds);
221+
}
222+
223+
public bool Set(string key, object value, int cacheSeconds)
224+
{
225+
return _memcachedClient.Set(key, value, cacheSeconds);
226+
}
227+
228+
public Task<bool> SetAsync(string key, object value, int cacheSeconds)
229+
{
230+
return _memcachedClient.SetAsync(key, value, cacheSeconds);
231+
}
232+
233+
public ServerStats Stats()
234+
{
235+
return _memcachedClient.Stats();
236+
}
237+
238+
public ServerStats Stats(string type)
239+
{
240+
return _memcachedClient.Stats(type);
241+
}
242+
243+
public bool Store(StoreMode mode, string key, object value)
244+
{
245+
return _memcachedClient.Store(mode, key, value);
246+
}
247+
248+
public bool Store(StoreMode mode, string key, object value, DateTime expiresAt)
249+
{
250+
return _memcachedClient.Store(mode, key, value, expiresAt);
251+
}
252+
253+
public bool Store(StoreMode mode, string key, object value, TimeSpan validFor)
254+
{
255+
return _memcachedClient.Store(mode, key, value, validFor);
256+
}
257+
258+
public Task<bool> StoreAsync(StoreMode mode, string key, object value, DateTime expiresAt)
259+
{
260+
return _memcachedClient.StoreAsync(mode, key, value, expiresAt);
261+
}
262+
263+
public Task<bool> StoreAsync(StoreMode mode, string key, object value, TimeSpan validFor)
264+
{
265+
return _memcachedClient.StoreAsync(mode, key, value, validFor);
266+
}
267+
268+
public Task<IOperationResult> TouchAsync(string key, DateTime expiresAt)
269+
{
270+
return _memcachedClient.TouchAsync(key, expiresAt);
271+
}
272+
273+
public Task<IOperationResult> TouchAsync(string key, TimeSpan validFor)
274+
{
275+
return _memcachedClient.TouchAsync(key, validFor);
276+
}
277+
278+
public bool TryGet(string key, out object value)
279+
{
280+
return _memcachedClient.TryGet(key, out value);
281+
}
282+
283+
public bool TryGetWithCas(string key, out CasResult<object> value)
284+
{
285+
return _memcachedClient.TryGetWithCas(key, out value);
286+
}
287+
}
288+
}

SampleWebApp.IntegrationTests/HomeControllerTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,13 @@ public async Task HomeController_Index()
3737

3838
await memcachedClient.RemoveAsync(HomeController.CacheKey);
3939
}
40+
41+
[Fact]
42+
public async Task Get_postbody_from_cache_ok()
43+
{
44+
var httpClient = _factory.CreateClient();
45+
var response = await httpClient.GetAsync("/home/postbody");
46+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
47+
}
4048
}
4149
}

0 commit comments

Comments
 (0)