Skip to content

Commit

Permalink
Splitting into separate Abstractions package and updating samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaabreu committed Oct 9, 2020
1 parent a8c47a2 commit b0b3e92
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 99 deletions.
14 changes: 14 additions & 0 deletions SimpleConcepts.Extensions.Caching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{56462026-DB0
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{B1B8FE9E-ABDC-4B31-B535-1D88E04DECCE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleConcepts.Extensions.Caching.Abstractions", "src\SimpleConcepts.Extensions.Caching.Abstractions\SimpleConcepts.Extensions.Caching.Abstractions.csproj", "{8068196F-7A92-4790-995F-FA1CE5687DED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Application", "samples\Sample.Application\Sample.Application.csproj", "{58CBD1DE-41EE-4BAA-9984-D5BEA8E483B2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -38,6 +42,14 @@ Global
{270D3C88-0150-45AD-B263-F7154ABAF3A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{270D3C88-0150-45AD-B263-F7154ABAF3A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{270D3C88-0150-45AD-B263-F7154ABAF3A9}.Release|Any CPU.Build.0 = Release|Any CPU
{8068196F-7A92-4790-995F-FA1CE5687DED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8068196F-7A92-4790-995F-FA1CE5687DED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8068196F-7A92-4790-995F-FA1CE5687DED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8068196F-7A92-4790-995F-FA1CE5687DED}.Release|Any CPU.Build.0 = Release|Any CPU
{58CBD1DE-41EE-4BAA-9984-D5BEA8E483B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58CBD1DE-41EE-4BAA-9984-D5BEA8E483B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58CBD1DE-41EE-4BAA-9984-D5BEA8E483B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58CBD1DE-41EE-4BAA-9984-D5BEA8E483B2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -46,6 +58,8 @@ Global
{A0B92D7E-A88E-4115-A569-25ACE8481F68} = {B1B8FE9E-ABDC-4B31-B535-1D88E04DECCE}
{531D00D9-92C6-4E94-92CB-E94B97E45335} = {56462026-DB0C-4F3A-A4E9-185C32C9BA95}
{270D3C88-0150-45AD-B263-F7154ABAF3A9} = {E009AB93-C201-45BD-B4BB-F450F55F112F}
{8068196F-7A92-4790-995F-FA1CE5687DED} = {56462026-DB0C-4F3A-A4E9-185C32C9BA95}
{58CBD1DE-41EE-4BAA-9984-D5BEA8E483B2} = {E009AB93-C201-45BD-B4BB-F450F55F112F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15C770B8-637D-429B-ACAC-7E6058EE356C}
Expand Down
64 changes: 64 additions & 0 deletions samples/Sample.Application/DistributedWeatherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;

namespace Sample.Application
{
public class DistributedWeatherService : IDistributedWeatherService
{
private static readonly Random RNG = new Random();
private static readonly string[] SUMMARIES = {
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly IDistributedCache _distributedCache;

private readonly DistributedCacheEntryOptions _entryOptions = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(15)
};

public DistributedWeatherService(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}

public async Task<IEnumerable<WeatherForecast>> FetchAsync(CancellationToken cancellationToken)
{
var forecasts = new List<WeatherForecast>();

for (var index = 1; index < 5; index++)
{
var date = DateTime.Now.Date.AddDays(index);

// Get cached daily forecast if it exists and fetch if not.
var forecast = await _distributedCache
.GetOrSetJsonObjectAsync($"single-weather-forecast:{date.ToShortDateString()}",
() => FetchSingleAsync(date, cancellationToken),
_entryOptions,
cancellationToken);

forecasts.Add(forecast);
}

return forecasts.AsEnumerable();
}

private async Task<WeatherForecast> FetchSingleAsync(DateTime date, CancellationToken cancellationToken)
{
// Simulate access to a database or third party service.
await Task.Delay(100, cancellationToken);

// Return mock result.
return new WeatherForecast
{
Date = date,
TemperatureC = RNG.Next(-20, 55),
Summary = SUMMARIES[RNG.Next(SUMMARIES.Length)]
};
}
}
}
11 changes: 11 additions & 0 deletions samples/Sample.Application/IDistributedWeatherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Sample.Application
{
public interface IDistributedWeatherService
{
Task<IEnumerable<WeatherForecast>> FetchAsync(CancellationToken cancellationToken);
}
}
11 changes: 11 additions & 0 deletions samples/Sample.Application/ISimpleWeatherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Sample.Application
{
public interface ISimpleWeatherService
{
Task<IEnumerable<WeatherForecast>> FetchAsync(CancellationToken cancellationToken);
}
}
11 changes: 11 additions & 0 deletions samples/Sample.Application/Sample.Application.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\SimpleConcepts.Extensions.Caching.Abstractions\SimpleConcepts.Extensions.Caching.Abstractions.csproj" />
</ItemGroup>

</Project>
57 changes: 57 additions & 0 deletions samples/Sample.Application/SimpleWeatherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SimpleConcepts.Extensions.Caching;

namespace Sample.Application
{
public class SimpleWeatherService : ISimpleWeatherService
{
private static readonly Random RNG = new Random();
private static readonly string[] SUMMARIES = {
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ISimpleCache<DateTime, WeatherForecast> _dailyForecastCache;

public SimpleWeatherService(ISimpleCache<DateTime, WeatherForecast> dailyForecastCache)
{
_dailyForecastCache = dailyForecastCache;
}

public async Task<IEnumerable<WeatherForecast>> FetchAsync(CancellationToken cancellationToken)
{
var forecasts = new List<WeatherForecast>();

for (var index = 1; index < 5; index++)
{
var date = DateTime.Now.Date.AddDays(index);

// Get cached daily forecast if it exists and fetch if not.
var forecast = await _dailyForecastCache
.GetOrSetAsync(date, () => FetchSingleAsync(date, cancellationToken), cancellationToken);

forecasts.Add(forecast);
}

return forecasts.AsEnumerable();
}

private async Task<WeatherForecast> FetchSingleAsync(DateTime date, CancellationToken cancellationToken)
{
// Simulate access to a database or third party service.
await Task.Delay(100, cancellationToken);

// Return mock result.
return new WeatherForecast
{
Date = date,
TemperatureC = RNG.Next(-20, 55),
Summary = SUMMARIES[RNG.Next(SUMMARIES.Length)]
};
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Sample.Web
namespace Sample.Application
{
public class WeatherForecast
{
Expand Down
53 changes: 5 additions & 48 deletions samples/Sample.Web/Controllers/DistributedCacheController.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Sample.Application;

namespace Sample.Web.Controllers
{
[ApiController]
[Route("[controller]")]
public class DistributedCacheController : ControllerBase
{
private static readonly Random rng = new Random();

private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly IDistributedCache _distributedCache;
private readonly ILogger<SimpleCacheController> _logger;
private readonly IDistributedWeatherService _distributedWeatherService;

public DistributedCacheController(
IDistributedCache distributedCache,
ILogger<SimpleCacheController> logger
IDistributedWeatherService distributedWeatherService
)
{
_distributedCache = distributedCache;
_logger = logger;
_distributedWeatherService = distributedWeatherService;
}

[HttpGet]
Expand All @@ -38,46 +30,11 @@ public async Task<IEnumerable<WeatherForecast>> GetAsync(CancellationToken cance
// Will get cached response if there is any and fetch otherwise.
var response = await _distributedCache
.GetOrSetJsonObjectAsync("WeatherForecastController_Get",
() => FetchAllForecastsAsync(cancellationToken),
() => _distributedWeatherService.FetchAsync(cancellationToken),
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5) },
cancellationToken);

return response;
}

private async Task<IEnumerable<WeatherForecast>> FetchAllForecastsAsync(CancellationToken cancellationToken)
{
var forecasts = new List<WeatherForecast>();

for (var index = 1; index < 5; index++)
{
var date = DateTime.Now.Date.AddDays(index);

// Get cached daily forecast if it exists and fetch if not.
var forecast = await _distributedCache
.GetOrSetJsonObjectAsync($"single-weather-forecast:{date.ToShortDateString()}",
() => FetchSingleForecastAsync(date, cancellationToken),
new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(15) },
cancellationToken);

forecasts.Add(forecast);
}

return forecasts.AsEnumerable();
}

private async Task<WeatherForecast> FetchSingleForecastAsync(DateTime date, CancellationToken cancellationToken)
{
// Simulate access to a database or third party service.
await Task.Delay(100, cancellationToken);

// Return mock result.
return new WeatherForecast
{
Date = date,
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
};
}
}
}
56 changes: 6 additions & 50 deletions samples/Sample.Web/Controllers/SimpleCacheController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sample.Application;
using SimpleConcepts.Extensions.Caching;

namespace Sample.Web.Controllers
Expand All @@ -13,68 +11,26 @@ namespace Sample.Web.Controllers
[Route("[controller]")]
public class SimpleCacheController : ControllerBase
{
private static readonly Random rng = new Random();

private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ISimpleCache<IEnumerable<WeatherForecast>> _responseCache;
private readonly ISimpleCache<DateTime, WeatherForecast> _dailyForecastCache;
private readonly ILogger<SimpleCacheController> _logger;
private readonly ISimpleWeatherService _simpleWeatherService;

public SimpleCacheController(
ISimpleCache<IEnumerable<WeatherForecast>> responseCache,
ISimpleCache<DateTime, WeatherForecast> dailyForecastCache,
ILogger<SimpleCacheController> logger
ISimpleWeatherService simpleWeatherService
)
{
_responseCache = responseCache;
_dailyForecastCache = dailyForecastCache;
_logger = logger;
_simpleWeatherService = simpleWeatherService;
}

[HttpGet]
public async Task<IEnumerable<WeatherForecast>> GetAsync(CancellationToken cancellationToken)
{
// Will get cached response if there is any and fetch otherwise.
var response = await _responseCache
.GetOrSetAsync(() => FetchAllForecastsAsync(cancellationToken), cancellationToken);
.GetOrSetAsync(() => _simpleWeatherService.FetchAsync(cancellationToken), cancellationToken);

return response;
}

private async Task<IEnumerable<WeatherForecast>> FetchAllForecastsAsync(CancellationToken cancellationToken)
{
var forecasts = new List<WeatherForecast>();

for (var index = 1; index < 5; index++)
{
var date = DateTime.Now.Date.AddDays(index);

// Get cached daily forecast if it exists and fetch if not.
var forecast = await _dailyForecastCache
.GetOrSetAsync(date, () => FetchSingleForecastAsync(date, cancellationToken), cancellationToken);

forecasts.Add(forecast);
}

return forecasts.AsEnumerable();
}

private async Task<WeatherForecast> FetchSingleForecastAsync(DateTime date, CancellationToken cancellationToken)
{
// Simulate access to a database or third party service.
await Task.Delay(100, cancellationToken);

// Return mock result.
return new WeatherForecast
{
Date = date,
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
};
}
}
}
1 change: 1 addition & 0 deletions samples/Sample.Web/Sample.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\SimpleConcepts.Extensions.Caching\SimpleConcepts.Extensions.Caching.csproj" />
<ProjectReference Include="..\Sample.Application\Sample.Application.csproj" />
</ItemGroup>


Expand Down
Loading

0 comments on commit b0b3e92

Please sign in to comment.