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
2 changes: 1 addition & 1 deletion Enyim.Caching.Tests/MemcachedClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public MemcachedClientTestsBase(Action<MemcachedClientOptions> onAddEnyimMemcach
onAddEnyimMemcached?.Invoke(options);
});

services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Information).AddConsole());
IServiceProvider serviceProvider = services.BuildServiceProvider();
_client = serviceProvider.GetService<IMemcachedClient>() as MemcachedClient;
}
Expand Down
4 changes: 4 additions & 0 deletions Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
var result = await GetAsync<T>(key);
if (result.Success)
{
_logger.LogDebug($"Cache is hint. Key is '{key}'.");
return result.Value;
}
}
Expand All @@ -228,12 +229,15 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
_logger.LogError(ex, $"{nameof(GetAsync)}<{typeof(T)}>(\"{key}\")");
}

_logger.LogDebug($"Cache is missed. Key is '{key}'.");

var value = await generator?.Invoke();
if (value != null)
{
try
{
await AddAsync(key, value, cacheSeconds);
_logger.LogDebug($"Added value into cache. Key is '{key}'. " + value);
}
catch (Exception ex)
{
Expand Down
10 changes: 10 additions & 0 deletions EnyimCachingCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{46CD2BBA-9
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{9954C92E-50E2-477C-AC47-31C857C46370}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApp.IntegrationTests", "SampleWebApp.IntegrationTests\SampleWebApp.IntegrationTests.csproj", "{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,10 @@ Global
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B}.Release|Any CPU.Build.0 = Release|Any CPU
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -48,5 +54,9 @@ Global
{7F795CB9-6058-4137-B069-12C1B8D4132B} = {9954C92E-50E2-477C-AC47-31C857C46370}
{FAF5E655-9ED4-4934-94F9-F0EDB77143AC} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
{3DDAC26F-8B56-4F69-8C69-A3CB6CF6B12B} = {46CD2BBA-9846-42FA-823B-D03F68F81647}
{281CA49A-FEA5-4008-8D3D-0C4E1C548B8C} = {9954C92E-50E2-477C-AC47-31C857C46370}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D8C07FE8-52FF-48B8-A079-A180058AABC6}
EndGlobalSection
EndGlobal
41 changes: 41 additions & 0 deletions SampleWebApp.IntegrationTests/HomeControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.SampleWebApp;
using Enyim.Caching.SampleWebApp.Controllers;
using Enyim.Caching.SampleWebApp.Models;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace SampleWebApp.IntegrationTests
{
public class HomeControllerTests : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _factory;

public HomeControllerTests(WebApplicationFactory<Startup> factory)
{
_factory = factory;
}

[Fact]
public async Task HomeController_Index()
{
var httpClient = _factory.CreateClient();
var response = await httpClient.GetAsync("/");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var memcachedClient = _factory.Server.Host.Services.GetRequiredService<IMemcachedClient>();
var posts = await memcachedClient.GetValueAsync<IEnumerable<BlogPost>>(HomeController.CacheKey);
Assert.NotNull(posts);
Assert.NotEmpty(posts.First().Title);

await memcachedClient.RemoveAsync(HomeController.CacheKey);
}
}
}
27 changes: 27 additions & 0 deletions SampleWebApp.IntegrationTests/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57329/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleWebApp.IntegrationTests": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:57330/"
}
}
}
25 changes: 25 additions & 0 deletions SampleWebApp.IntegrationTests/SampleWebApp.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Enyim.Caching\Enyim.Caching.csproj" />
<ProjectReference Include="..\SampleWebApp\SampleWebApp.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions SampleWebApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.SampleWebApp.Models;
using Enyim.Caching.SampleWebApp.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Enyim.Caching.SampleWebApp.Controllers
{
public class HomeController : Controller
{
private readonly IMemcachedClient _memcachedClient;
private readonly IBlogPostService _blogPostService;
private readonly ILogger _logger;
public static readonly string CacheKey = "blogposts-recent";

public HomeController(
IMemcachedClient memcachedClient,
IBlogPostService blogPostService,
ILoggerFactory loggerFactory)
{
_memcachedClient = memcachedClient;
_blogPostService = blogPostService;
_logger = loggerFactory.CreateLogger<HomeController>();
}

public async Task<IActionResult> Index()
{
_logger.LogDebug("Executing _memcachedClient.GetValueOrCreateAsync...");

var cacheSeconds = 600;
var posts = await _memcachedClient.GetValueOrCreateAsync(
CacheKey,
cacheSeconds,
async () => await _blogPostService.GetRecent(10));

_logger.LogDebug("Done _memcachedClient.GetValueOrCreateAsync");

return Ok(posts);
}
}
}
13 changes: 13 additions & 0 deletions SampleWebApp/Models/BlogPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Enyim.Caching.SampleWebApp.Models
{
public class BlogPost
{
public string Title { get; set; }
public string Body { get; set; }
}
}
15 changes: 6 additions & 9 deletions SampleWebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace SampleWebApp
namespace Enyim.Caching.SampleWebApp
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
2 changes: 2 additions & 0 deletions SampleWebApp/SampleWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>Enyim.Caching.SampleWebApp</RootNamespace>
<AssemblyName>Enyim.Caching.SampleWebApp</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions SampleWebApp/Services/BlogPostService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Enyim.Caching.SampleWebApp.Models;

namespace Enyim.Caching.SampleWebApp.Services
{
public class BlogPostService : IBlogPostService
{
public async ValueTask<IEnumerable<BlogPost>> GetRecent(int itemCount)
{
return new List<BlogPost> { new BlogPost { Title = "Hello World", Body = "EnyimCachingCore" } };
}
}
}
13 changes: 13 additions & 0 deletions SampleWebApp/Services/IBlogPostService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Enyim.Caching.SampleWebApp.Models;

namespace Enyim.Caching.SampleWebApp.Services
{
public interface IBlogPostService
{
ValueTask<IEnumerable<BlogPost>> GetRecent(int itemCount);
}
}
56 changes: 12 additions & 44 deletions SampleWebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,41 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.SampleWebApp.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Enyim.Caching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Caching.Distributed;

namespace SampleWebApp
namespace Enyim.Caching.SampleWebApp
{
public class Startup
{
public Startup(IHostingEnvironment env)
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
IsDevelopment = env.IsDevelopment();
Configuration = configuration;
}

public IConfigurationRoot Configuration { get; set; }

public bool IsDevelopment { get; set; }
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddEnyimMemcached();
//services.AddEnyimMemcached(Configuration);
//services.AddEnyimMemcached(Configuration, "enyimMemcached");
//services.AddEnyimMemcached(Configuration.GetSection("enyimMemcached"));
services.AddTransient<IBlogPostService, BlogPostService>();
services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app)
{
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";
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)
{
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
{
var message = $"Missed cache with '{cacheKey}' key";
await context.Response.WriteAsync(message);
logger.LogError(message);
}
});
app.UseMvcWithDefaultRoute();
}
}
}
5 changes: 3 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
dotnet test Enyim.Caching.Tests/Enyim.Caching.Tests.csproj -c Release -v normal
dotnet test MemcachedTest/MemcachedTest.csproj -c Release -v normal
dotnet test SampleWebApp.IntegrationTests/*.csproj -c Release
dotnet test Enyim.Caching.Tests/*.csproj -c Release
dotnet test MemcachedTest/*.csproj -c Release