Started working on output caching api results
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.OutputCaching;
|
||||||
using Portfolio.Shared.Services;
|
using Portfolio.Shared.Services;
|
||||||
|
|
||||||
namespace Portfolio.Api.Controller;
|
namespace Portfolio.Api.Controller;
|
||||||
|
|
||||||
[ApiController, Route("api/about")]
|
[ApiController, Route("api/about"), OutputCache(Tags = [DatabaseContext.CacheKey])]
|
||||||
public class AboutController(IAboutRepository repository) : ControllerBase {
|
public class AboutController(IAboutRepository repository) : ControllerBase {
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.OutputCaching;
|
||||||
using Portfolio.Shared.Services;
|
using Portfolio.Shared.Services;
|
||||||
|
|
||||||
namespace Portfolio.Api.Controller;
|
namespace Portfolio.Api.Controller;
|
||||||
|
|
||||||
[ApiController, Route("api/projects")]
|
[ApiController, Route("api/projects"), OutputCache(Tags = [DatabaseContext.CacheKey])]
|
||||||
public class ProjectController(IProjectRepository repository) : ControllerBase {
|
public class ProjectController(IProjectRepository repository) : ControllerBase {
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.OutputCaching;
|
||||||
using Portfolio.Shared.Services;
|
using Portfolio.Shared.Services;
|
||||||
|
|
||||||
namespace Portfolio.Api.Controller;
|
namespace Portfolio.Api.Controller;
|
||||||
|
|
||||||
[ApiController, Route("api/technologies")]
|
[ApiController, Route("api/technologies"), OutputCache(Tags = [DatabaseContext.CacheKey])]
|
||||||
public class TechnologyController(ITechnologyRepository repository) : ControllerBase {
|
public class TechnologyController(ITechnologyRepository repository) : ControllerBase {
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.OutputCaching;
|
||||||
using Portfolio.Shared.Models;
|
using Portfolio.Shared.Models;
|
||||||
using Portfolio.Shared.Services;
|
using Portfolio.Shared.Services;
|
||||||
|
|
||||||
namespace Portfolio.Api.Controller;
|
namespace Portfolio.Api.Controller;
|
||||||
|
|
||||||
[ApiController, Route("api/timeline")]
|
[ApiController, Route("api/timeline"), OutputCache(Tags = [DatabaseContext.CacheKey])]
|
||||||
public class TimelineController(ITimelineRepository repository) : ControllerBase {
|
public class TimelineController(ITimelineRepository repository) : ControllerBase {
|
||||||
|
|
||||||
[HttpGet("{type}")]
|
[HttpGet("{type}")]
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.AspNetCore.OutputCaching;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Portfolio.Shared.Models;
|
using Portfolio.Shared.Models;
|
||||||
|
|
||||||
namespace Portfolio.Api;
|
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; }
|
public DbSet<Project> Projects { get; set; }
|
||||||
|
|
||||||
@@ -19,5 +22,9 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbCont
|
|||||||
.WithMany()
|
.WithMany()
|
||||||
.UsingEntity("LanguageProject");
|
.UsingEntity("LanguageProject");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new()) {
|
||||||
|
await cacheStore.EvictByTagAsync(CacheKey, cancellationToken);
|
||||||
|
return await base.SaveChangesAsync(cancellationToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0" />
|
<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="HopFrame.Web" Version="3.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using HopFrame.Core.Config;
|
using HopFrame.Core.Config;
|
||||||
using HopFrame.Web;
|
using HopFrame.Web;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Portfolio.Api;
|
using Portfolio.Api;
|
||||||
using Portfolio.Api.Services;
|
using Portfolio.Api.Services;
|
||||||
using Portfolio.Shared.Models;
|
using Portfolio.Shared.Models;
|
||||||
@@ -23,6 +22,11 @@ builder.Services.AddScoped<IAboutRepository, AboutRepository>();
|
|||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
builder.AddRedisOutputCache("cache");
|
||||||
|
builder.Services.AddOutputCache(options => {
|
||||||
|
options.DefaultExpirationTimeSpan = TimeSpan.FromDays(30);
|
||||||
|
});
|
||||||
|
|
||||||
builder.Services.AddHopFrame(options => {
|
builder.Services.AddHopFrame(options => {
|
||||||
options.DisplayUserInfo(false);
|
options.DisplayUserInfo(false);
|
||||||
options.AddDbContext<DatabaseContext>(context => {
|
options.AddDbContext<DatabaseContext>(context => {
|
||||||
@@ -94,6 +98,8 @@ await using (var scope = app.Services.CreateAsyncScope()) {
|
|||||||
|
|
||||||
app.MapDefaultEndpoints();
|
app.MapDefaultEndpoints();
|
||||||
|
|
||||||
|
app.UseOutputCache();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.UseAntiforgery();
|
app.UseAntiforgery();
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0"/>
|
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0"/>
|
||||||
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
|
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Aspire.Hosting.Redis" Version="9.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -5,9 +5,13 @@ var postgres = builder.AddPostgres("postgres")
|
|||||||
|
|
||||||
var db = postgres.AddDatabase("data");
|
var db = postgres.AddDatabase("data");
|
||||||
|
|
||||||
|
var cache = builder.AddRedis("cache");
|
||||||
|
|
||||||
var api = builder.AddProject<Projects.Portfolio_Api>("api")
|
var api = builder.AddProject<Projects.Portfolio_Api>("api")
|
||||||
.WithReference(db)
|
.WithReference(db)
|
||||||
.WaitFor(db);
|
.WaitFor(db)
|
||||||
|
.WithReference(cache)
|
||||||
|
.WaitFor(cache);
|
||||||
|
|
||||||
builder.AddProject<Projects.Portfolio_Web>("web")
|
builder.AddProject<Projects.Portfolio_Web>("web")
|
||||||
.WithReference(api)
|
.WithReference(api)
|
||||||
|
|||||||
Reference in New Issue
Block a user