Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,11 @@ private static IServiceCollection AddEnyimMemcached(IServiceCollection services,
{
services.AddOptions();
configure(services);
services.Add(ServiceDescriptor.Transient<IMemcachedClientConfiguration, MemcachedClientConfiguration>());
services.Add(ServiceDescriptor.Singleton<IMemcachedClient, MemcachedClient>());
return services;
}

public static IServiceCollection AddDistributedEnyimMemcached(this IServiceCollection services, Action<MemcachedClientOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddTransient<IMemcachedClientConfiguration, MemcachedClientConfiguration>();
services.AddSingleton<MemcachedClient, MemcachedClient>();

if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}

services.AddOptions();
services.Configure(setupAction);
services.Add(ServiceDescriptor.Transient<IMemcachedClientConfiguration, MemcachedClientConfiguration>());
services.Add(ServiceDescriptor.Singleton<IDistributedCache, MemcachedClient>());
services.AddSingleton<IMemcachedClient>(factory => factory.GetService<MemcachedClient>());
services.AddSingleton<IDistributedCache>(factory => factory.GetService<MemcachedClient>());

return services;
}
Expand Down
12 changes: 7 additions & 5 deletions SampleWebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;

namespace SampleWebApp
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}
6 changes: 6 additions & 0 deletions SampleWebApp/SampleWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
<ProjectReference Include="..\Enyim.Caching\Enyim.Caching.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
13 changes: 9 additions & 4 deletions SampleWebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Enyim.Caching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Caching.Distributed;

namespace SampleWebApp
{
Expand Down Expand Up @@ -36,22 +37,26 @@ public void ConfigureServices(IServiceCollection services)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);

app.UseEnyimMemcached();

var memcachedClient = app.ApplicationServices.GetService<IMemcachedClient>();
var distributedCache = app.ApplicationServices.GetService<IDistributedCache>();
var logger = loggerFactory.CreateLogger<MemcachedClient>();

app.Run(async (context) =>
{
var cacheKey = "sample_response";
await memcachedClient.AddAsync(cacheKey, "Hello World!", 60);
var distributedCaceKey = "distributed_" + cacheKey;
await memcachedClient.AddAsync(cacheKey, $"Hello World from {nameof(memcachedClient)}!", 60);
await distributedCache.SetStringAsync(distributedCaceKey,$"Hello World from {nameof(distributedCache)}!");
var cacheResult = await memcachedClient.GetAsync<string>(cacheKey);
if (cacheResult.Success)
{
await context.Response.WriteAsync(cacheResult.Value);
var distributedCacheValue = await distributedCache.GetStringAsync(distributedCaceKey);
await context.Response
.WriteAsync($"memcachedClient: {cacheResult.Value}\ndistributedCache: {distributedCacheValue}");
await memcachedClient.RemoveAsync(cacheKey);
await distributedCache.RemoveAsync(distributedCaceKey);
logger.LogDebug($"Hinted cache with '{cacheKey}' key");
}
else
Expand Down
8 changes: 8 additions & 0 deletions SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
// "password": "password"
// }
//}
},

"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Warning",
"Microsoft": "Warning"
}
}
}