Skip to content

Commit 11e54fc

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

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,11 @@ private static IServiceCollection AddEnyimMemcached(IServiceCollection services,
4646
{
4747
services.AddOptions();
4848
configure(services);
49-
services.Add(ServiceDescriptor.Transient<IMemcachedClientConfiguration, MemcachedClientConfiguration>());
50-
services.Add(ServiceDescriptor.Singleton<IMemcachedClient, MemcachedClient>());
51-
return services;
52-
}
53-
54-
public static IServiceCollection AddDistributedEnyimMemcached(this IServiceCollection services, Action<MemcachedClientOptions> setupAction)
55-
{
56-
if (services == null)
57-
{
58-
throw new ArgumentNullException(nameof(services));
59-
}
49+
services.AddTransient<IMemcachedClientConfiguration, MemcachedClientConfiguration>();
50+
services.AddSingleton<MemcachedClient, MemcachedClient>();
6051

61-
if (setupAction == null)
62-
{
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>());
52+
services.AddSingleton<IMemcachedClient>(factory => factory.GetService<MemcachedClient>());
53+
services.AddSingleton<IDistributedCache>(factory => factory.GetService<MemcachedClient>());
7054

7155
return services;
7256
}

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: 9 additions & 4 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
{
@@ -36,22 +37,26 @@ public void ConfigureServices(IServiceCollection services)
3637

3738
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3839
{
39-
loggerFactory.AddConsole(LogLevel.Debug);
40-
4140
app.UseEnyimMemcached();
4241

4342
var memcachedClient = app.ApplicationServices.GetService<IMemcachedClient>();
43+
var distributedCache = app.ApplicationServices.GetService<IDistributedCache>();
4444
var logger = loggerFactory.CreateLogger<MemcachedClient>();
4545

4646
app.Run(async (context) =>
4747
{
4848
var cacheKey = "sample_response";
49-
await memcachedClient.AddAsync(cacheKey, "Hello World!", 60);
49+
var distributedCaceKey = "distributed_" + cacheKey;
50+
await memcachedClient.AddAsync(cacheKey, $"Hello World from {nameof(memcachedClient)}!", 60);
51+
await distributedCache.SetStringAsync(distributedCaceKey,$"Hello World from {nameof(distributedCache)}!");
5052
var cacheResult = await memcachedClient.GetAsync<string>(cacheKey);
5153
if (cacheResult.Success)
5254
{
53-
await context.Response.WriteAsync(cacheResult.Value);
55+
var distributedCacheValue = await distributedCache.GetStringAsync(distributedCaceKey);
56+
await context.Response
57+
.WriteAsync($"memcachedClient: {cacheResult.Value}\ndistributedCache: {distributedCacheValue}");
5458
await memcachedClient.RemoveAsync(cacheKey);
59+
await distributedCache.RemoveAsync(distributedCaceKey);
5560
logger.LogDebug($"Hinted cache with '{cacheKey}' key");
5661
}
5762
else

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)