Skip to content

Commit

Permalink
Adicionado cache e compressao
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebaltieri committed Nov 10, 2021
1 parent c65e7c0 commit a09db54
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Modulo 6/Blog/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
using Blog.ViewModels.Categories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;

namespace Blog.Controllers
{
[ApiController]
public class CategoryController : ControllerBase
{
[HttpGet("v1/categories")]
public async Task<IActionResult> GetAsync(
public IActionResult GetAsync(
[FromServices] IMemoryCache cache,
[FromServices] BlogDataContext context)
{
try
{
var categories = await context.Categories.ToListAsync();
var categories = cache.GetOrCreate("CategoriesCache", entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
return GetCategories(context);
});

return Ok(new ResultViewModel<List<Category>>(categories));
}
catch
Expand All @@ -26,6 +33,11 @@ public async Task<IActionResult> GetAsync(
}
}

private List<Category> GetCategories(BlogDataContext context)
{
return context.Categories.ToList();
}

[HttpGet("v1/categories/{id:int}")]
public async Task<IActionResult> GetByIdAsync(
[FromRoute] int id,
Expand Down
44 changes: 44 additions & 0 deletions Modulo 6/Blog/Controllers/PostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task<IActionResult> DetailsAsync(
.Posts
.AsNoTracking()
.Include(x => x.Author)
.ThenInclude(x => x.Roles)
.Include(x => x.Category)
.FirstOrDefaultAsync(x => x.Id == id);

Expand All @@ -70,6 +71,49 @@ public async Task<IActionResult> DetailsAsync(

return Ok(new ResultViewModel<Post>(post));
}
catch (Exception ex)
{
return StatusCode(500, new ResultViewModel<List<Category>>("05X04 - Falha interna no servidor"));
}
}

[HttpGet("v1/posts/category/{category}")]
public async Task<IActionResult> GetByCategoryAsync(
[FromRoute] string category,
[FromServices] BlogDataContext context,
[FromQuery] int page = 0,
[FromQuery] int pageSize = 25)
{
try
{
var count = await context.Posts.AsNoTracking().CountAsync();
var posts = await context
.Posts
.AsNoTracking()
.Include(x => x.Author)
.Include(x => x.Category)
.Where(x => x.Category.Slug == category)
.Select(x => new ListPostsViewModel
{
Id = x.Id,
Title = x.Title,
Slug = x.Slug,
LastUpdateDate = x.LastUpdateDate,
Category = x.Category.Name,
Author = $"{x.Author.Name} ({x.Author.Email})"
})
.Skip(page * pageSize)
.Take(pageSize)
.OrderByDescending(x => x.LastUpdateDate)
.ToListAsync();
return Ok(new ResultViewModel<dynamic>(new
{
total = count,
page,
pageSize,
posts
}));
}
catch
{
return StatusCode(500, new ResultViewModel<List<Category>>("05X04 - Falha interna no servidor"));
Expand Down
14 changes: 14 additions & 0 deletions Modulo 6/Blog/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.IO.Compression;
using System.Text;
using System.Text.Json.Serialization;
using Blog;
using Blog.Data;
using Blog.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.IdentityModel.Tokens;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -18,6 +20,7 @@
app.UseAuthorization();
app.MapControllers();
app.UseStaticFiles();
app.UseResponseCompression();
app.Run();

void LoadConfiguration(WebApplication app)
Expand Down Expand Up @@ -52,6 +55,17 @@ void ConfigureAuthentication(WebApplicationBuilder builder)

void ConfigureMvc(WebApplicationBuilder builder)
{
builder.Services.AddMemoryCache();
builder.Services.AddResponseCompression(options =>
{
// options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
// options.Providers.Add<CustomCompressionProvider>();
});
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Optimal;
});
builder
.Services
.AddControllers()
Expand Down

0 comments on commit a09db54

Please sign in to comment.