Skip to content

Commit dc49ed9

Browse files
committed
Improve to work with IDistributedCache
1 parent bfaeecd commit dc49ed9

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,28 @@ private static IServiceCollection AddEnyimMemcached(IServiceCollection services,
4848
configure(services);
4949
services.Add(ServiceDescriptor.Transient<IMemcachedClientConfiguration, MemcachedClientConfiguration>());
5050
services.Add(ServiceDescriptor.Singleton<IMemcachedClient, MemcachedClient>());
51+
services.Add(ServiceDescriptor.Singleton<IDistributedCache, MemcachedClient>());
5152
return services;
5253
}
5354

54-
public static IServiceCollection AddDistributedEnyimMemcached(this IServiceCollection services, Action<MemcachedClientOptions> setupAction)
55+
public static IServiceCollection AddEnyimMemcachedIDistributedCache(this IServiceCollection services)
5556
{
56-
if (services == null)
57-
{
58-
throw new ArgumentNullException(nameof(services));
59-
}
60-
61-
if (setupAction == null)
57+
services.AddSingleton<IDistributedCache>(factory =>
6258
{
63-
throw new ArgumentNullException(nameof(setupAction));
64-
}
65-
66-
services.AddOptions();
67-
services.Configure(setupAction);
68-
services.Add(ServiceDescriptor.Transient<IMemcachedClientConfiguration, MemcachedClientConfiguration>());
69-
services.Add(ServiceDescriptor.Singleton<IDistributedCache, MemcachedClient>());
70-
59+
var memcachedClient = factory.GetService<IMemcachedClient>();
60+
if(memcachedClient == null)
61+
{
62+
throw new ArgumentNullException("Please AddEnyimMemcached first");
63+
}
64+
return memcachedClient as MemcachedClient;
65+
});
7166
return services;
7267
}
68+
69+
[Obsolete]
70+
public static IServiceCollection AddDistributedEnyimMemcached(this IServiceCollection services, Action<MemcachedClientOptions> setupAction)
71+
{
72+
return services.AddEnyimMemcachedIDistributedCache();
73+
}
7374
}
7475
}

SampleWebApp/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
using System.Linq;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore;
78

89
namespace SampleWebApp
910
{
1011
public class Program
1112
{
1213
public static void Main(string[] args)
1314
{
14-
var host = new WebHostBuilder()
15-
.UseKestrel()
16-
.UseContentRoot(Directory.GetCurrentDirectory())
15+
BuildWebHost(args).Run();
16+
}
17+
18+
public static IWebHost BuildWebHost(string[] args)
19+
{
20+
return WebHost.CreateDefaultBuilder(args)
1721
.UseStartup<Startup>()
1822
.Build();
19-
20-
host.Run();
2123
}
2224
}
2325
}

SampleWebApp/SampleWebApp.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
<ProjectReference Include="..\Enyim.Caching\Enyim.Caching.csproj" />
1414
</ItemGroup>
1515

16+
<ItemGroup>
17+
<Content Update="appsettings.json">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</Content>
20+
</ItemGroup>
21+
1622
</Project>

SampleWebApp/Startup.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Logging;
1010
using Enyim.Caching;
1111
using Microsoft.Extensions.Configuration;
12+
using Microsoft.Extensions.Caching.Distributed;
1213

1314
namespace SampleWebApp
1415
{
@@ -32,15 +33,15 @@ public Startup(IHostingEnvironment env)
3233
public void ConfigureServices(IServiceCollection services)
3334
{
3435
services.AddEnyimMemcached(Configuration.GetSection("enyimMemcached"));
36+
//.AddEnyimMemcachedIDistributedCache(); //For Microsoft.Extensions.Caching.Distributed.IDistributedCache
3537
}
3638

3739
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3840
{
39-
loggerFactory.AddConsole(LogLevel.Debug);
40-
4141
app.UseEnyimMemcached();
4242

4343
var memcachedClient = app.ApplicationServices.GetService<IMemcachedClient>();
44+
var distributedCache = app.ApplicationServices.GetService<IDistributedCache>();
4445
var logger = loggerFactory.CreateLogger<MemcachedClient>();
4546

4647
app.Run(async (context) =>
@@ -50,7 +51,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5051
var cacheResult = await memcachedClient.GetAsync<string>(cacheKey);
5152
if (cacheResult.Success)
5253
{
53-
await context.Response.WriteAsync(cacheResult.Value);
54+
var distributedCacheValue = await distributedCache.GetStringAsync(cacheKey);
55+
await context.Response
56+
.WriteAsync($"memcachedClient: {cacheResult.Value}<br/>distributedCache: {distributedCacheValue}");
5457
await memcachedClient.RemoveAsync(cacheKey);
5558
logger.LogDebug($"Hinted cache with '{cacheKey}' key");
5659
}

SampleWebApp/appsettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
// "password": "password"
2626
// }
2727
//}
28+
},
2829

30+
"Logging": {
31+
"IncludeScopes": false,
32+
"LogLevel": {
33+
"Default": "Debug",
34+
"System": "Warning",
35+
"Microsoft": "Warning"
36+
}
2937
}
3038
}

0 commit comments

Comments
 (0)