-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting into separate Abstractions package and updating samples.
- Loading branch information
1 parent
a8c47a2
commit b0b3e92
Showing
20 changed files
with
221 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
}; | ||
} | ||
|
||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
samples/Sample.Web/WeatherForecast.cs → ...les/Sample.Application/WeatherForecast.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.