Added caching to UserController pfp endpoint

This commit is contained in:
Mia Rose Winter 2024-02-03 20:29:37 +01:00
parent 38f02d0b7c
commit 39d7a0f15a
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E

View file

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Wave.Data; using Wave.Data;
using Wave.Services; using Wave.Services;
@ -8,13 +8,15 @@
namespace Wave.Controllers; namespace Wave.Controllers;
[ApiController] [ApiController]
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]")] [Route("/api/[controller]")]
public class UserController(ImageService imageService, IDbContextFactory<ApplicationDbContext> contextFactory) : ControllerBase { public class UserController(ImageService imageService, IDbContextFactory<ApplicationDbContext> contextFactory) : ControllerBase {
private ImageService ImageService { get; } = imageService; private ImageService ImageService { get; } = imageService;
private IDbContextFactory<ApplicationDbContext> ContextFactory { get; } = contextFactory; private IDbContextFactory<ApplicationDbContext> ContextFactory { get; } = contextFactory;
[HttpGet] [HttpGet]
[Microsoft.AspNetCore.Mvc.Route("pfp/{userId}")] [OutputCache(Duration = 60*5)]
[ResponseCache(Duration = 60*5, Location = ResponseCacheLocation.Any)]
[Route("pfp/{userId}")]
public async Task<IActionResult> Get(string userId) { public async Task<IActionResult> Get(string userId) {
await using var context = await ContextFactory.CreateDbContextAsync(); await using var context = await ContextFactory.CreateDbContextAsync();
var user = await context.Users.Include(u => u.ProfilePicture).FirstOrDefaultAsync(u => u.Id == userId); var user = await context.Users.Include(u => u.ProfilePicture).FirstOrDefaultAsync(u => u.Id == userId);