Skip to content

Commit

Permalink
#31: Created Cahching mechanism for one of the Users endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilanjansen committed May 29, 2019
1 parent 6c97be8 commit a5718f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
28 changes: 24 additions & 4 deletions EToken.API/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using EToken.Core.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Memory;

namespace EToken.API.Controllers
{
Expand All @@ -29,17 +30,21 @@ public class UsersController : ControllerBase
private IMapper _mapper;
private readonly AppSettings _appSettings;
private readonly ILogger<UsersController> _logger;

private readonly IMemoryCache _cache;
private string _userListKey="UsersList";
private string _userKey = "User";
public UsersController(
IUserService userService
,IMapper mapper
,IOptions<AppSettings> appSetting
,ILogger<UsersController> logger)
,ILogger<UsersController> logger
,IMemoryCache cache)
{
_userService = userService;
_mapper = mapper;
_appSettings = appSetting.Value;
_logger = logger;
_cache = cache;
}
/// <summary>
/// Authenticate a user Resource
Expand Down Expand Up @@ -123,8 +128,23 @@ public async Task<IActionResult> Register([FromBody]UserResource userResource)
[HttpGet]
public async Task<IActionResult> GetAll()
{
var users = await _userService.GetAllAsync();
var userResources = _mapper.Map<IList<UserResource>>(users);
IEnumerable<User> users = new List<User>();
if(!_cache.TryGetValue(_userListKey,out users))
{
if(users == null)
{
users= await _userService.GetAllAsync();
}
_cache.Set(_userListKey, users,
new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(5))
.SetAbsoluteExpiration(TimeSpan.FromHours(1)));
_logger.LogInformation($"{_userListKey} generated and set in cache.");
}
{
_logger.LogInformation($"{_userListKey} was available and pulled from cache.");
}
var userResources = _mapper.Map<IList<UserResource>>(users);
return Ok(userResources);
}
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions EToken.API/EToken.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include ="Serilog.AspNetCore" Version="2.1.1"/>
<PackageReference Include ="Serilog.Settings.Configuration" Version="3.0.1"/>
<PackageReference Include ="Serilog.Sinks.RollingFile" Version="3.3.0"/>
<PackageReference Include="Microsoft.AspNetCore.ResponseCaching" Version=""/>
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions EToken.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
// cache in memory
services.AddMemoryCache();
// caching response for middlewares
services.AddResponseCaching();
services.AddDbContext<ETokenDBContext>(x => x.UseInMemoryDatabase("TestDb"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
var config = new AutoMapper.MapperConfiguration(cfg =>
Expand Down Expand Up @@ -132,6 +136,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFa
loggerFactory.AddSerilog();
app.UseCors(x=>x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
app.UseAuthentication();
app.UseResponseCaching();
app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger();
Expand Down

0 comments on commit a5718f7

Please sign in to comment.