Started working on output caching api results

This commit is contained in:
2025-01-26 15:06:06 +01:00
parent 2dd3f7f5be
commit 97805795b7
9 changed files with 32 additions and 9 deletions

View File

@@ -1,9 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Portfolio.Shared.Services;
namespace Portfolio.Api.Controller;
[ApiController, Route("api/about")]
[ApiController, Route("api/about"), OutputCache(Tags = [DatabaseContext.CacheKey])]
public class AboutController(IAboutRepository repository) : ControllerBase {
[HttpGet]

View File

@@ -1,9 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Portfolio.Shared.Services;
namespace Portfolio.Api.Controller;
[ApiController, Route("api/projects")]
[ApiController, Route("api/projects"), OutputCache(Tags = [DatabaseContext.CacheKey])]
public class ProjectController(IProjectRepository repository) : ControllerBase {
[HttpGet]

View File

@@ -1,9 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Portfolio.Shared.Services;
namespace Portfolio.Api.Controller;
[ApiController, Route("api/technologies")]
[ApiController, Route("api/technologies"), OutputCache(Tags = [DatabaseContext.CacheKey])]
public class TechnologyController(ITechnologyRepository repository) : ControllerBase {
[HttpGet]

View File

@@ -1,10 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
using Portfolio.Shared.Models;
using Portfolio.Shared.Services;
namespace Portfolio.Api.Controller;
[ApiController, Route("api/timeline")]
[ApiController, Route("api/timeline"), OutputCache(Tags = [DatabaseContext.CacheKey])]
public class TimelineController(ITimelineRepository repository) : ControllerBase {
[HttpGet("{type}")]

View File

@@ -1,9 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.EntityFrameworkCore;
using Portfolio.Shared.Models;
namespace Portfolio.Api;
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options) {
public class DatabaseContext(DbContextOptions<DatabaseContext> options, IOutputCacheStore cacheStore) : DbContext(options) {
public const string CacheKey = "portfolio-data";
public DbSet<Project> Projects { get; set; }
@@ -20,4 +23,8 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbCont
.UsingEntity("LanguageProject");
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new()) {
await cacheStore.EvictByTagAsync(CacheKey, cancellationToken);
return await base.SaveChangesAsync(cancellationToken);
}
}

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0" />
<PackageReference Include="Aspire.StackExchange.Redis.OutputCaching" Version="9.0.0" />
<PackageReference Include="HopFrame.Web" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
</ItemGroup>

View File

@@ -1,6 +1,5 @@
using HopFrame.Core.Config;
using HopFrame.Web;
using Microsoft.EntityFrameworkCore;
using Portfolio.Api;
using Portfolio.Api.Services;
using Portfolio.Shared.Models;
@@ -23,6 +22,11 @@ builder.Services.AddScoped<IAboutRepository, AboutRepository>();
builder.Services.AddControllers();
builder.AddRedisOutputCache("cache");
builder.Services.AddOutputCache(options => {
options.DefaultExpirationTimeSpan = TimeSpan.FromDays(30);
});
builder.Services.AddHopFrame(options => {
options.DisplayUserInfo(false);
options.AddDbContext<DatabaseContext>(context => {
@@ -94,6 +98,8 @@ await using (var scope = app.Services.CreateAsyncScope()) {
app.MapDefaultEndpoints();
app.UseOutputCache();
app.MapControllers();
app.UseAntiforgery();

View File

@@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0"/>
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.0.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -5,9 +5,13 @@ var postgres = builder.AddPostgres("postgres")
var db = postgres.AddDatabase("data");
var cache = builder.AddRedis("cache");
var api = builder.AddProject<Projects.Portfolio_Api>("api")
.WithReference(db)
.WaitFor(db);
.WaitFor(db)
.WithReference(cache)
.WaitFor(cache);
builder.AddProject<Projects.Portfolio_Web>("web")
.WithReference(api)