Merge pull request #5132 from mdmontesinos/sitemap-cache

resolves #4899: output cache for sitemap
This commit is contained in:
Shaun Walker
2025-03-03 13:22:46 -05:00
committed by GitHub
11 changed files with 138 additions and 3 deletions

View File

@ -0,0 +1,31 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Oqtane.Models;
using Oqtane.Services;
using Oqtane.Shared;
namespace Oqtane.Controllers
{
[Route(ControllerRoutes.ApiRoute)]
public class CacheController : Controller
{
private readonly ICacheService _cacheService;
public CacheController(ICacheService cacheService)
{
_cacheService = cacheService;
}
// DELETE api/<controller>/outputCache/evictByTag/{tag}
[HttpDelete("outputCache/evictByTag/{tag}")]
[Authorize(Roles = RoleNames.Admin)]
public async Task EvictOutputCacheByTag(string tag, CancellationToken cancellationToken = default)
{
await _cacheService.EvictOutputCacheByTag(tag, cancellationToken);
}
}
}

View File

@ -117,6 +117,7 @@ namespace Microsoft.Extensions.DependencyInjection
// services
services.AddTransient<ISiteService, ServerSiteService>();
services.AddTransient<ILocalizationCookieService, ServerLocalizationCookieService>();
services.AddTransient<ICacheService, ServerCacheService>();
// repositories
services.AddTransient<IModuleDefinitionRepository, ModuleDefinitionRepository>();

View File

@ -7,6 +7,7 @@ using System.Xml;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.Extensions.DependencyInjection;
using Oqtane.Enums;
using Oqtane.Infrastructure;
@ -19,6 +20,7 @@ using Oqtane.Shared;
namespace Oqtane.Pages
{
[AllowAnonymous]
[OutputCache(Duration = 300, Tags = [Constants.SitemapOutputCacheTag])]
public class SitemapModel : PageModel
{
private readonly IServiceProvider _serviceProvider;

View File

@ -0,0 +1,41 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.OutputCaching;
using Oqtane.Documentation;
using Oqtane.Enums;
using Oqtane.Infrastructure;
using Oqtane.Shared;
namespace Oqtane.Services
{
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class ServerCacheService : ICacheService
{
private readonly IOutputCacheStore _outputCacheStore;
private readonly ILogManager _logger;
private readonly IHttpContextAccessor _accessor;
public ServerCacheService(IOutputCacheStore outputCacheStore, ILogManager logger, IHttpContextAccessor accessor)
{
_outputCacheStore = outputCacheStore;
_logger = logger;
_accessor = accessor;
}
public async Task EvictOutputCacheByTag(string tag, CancellationToken cancellationToken = default)
{
if (_accessor.HttpContext.User.IsInRole(RoleNames.Admin))
{
await _outputCacheStore.EvictByTagAsync(tag, cancellationToken);
_logger.Log(LogLevel.Information, this, LogFunction.Other, "Evicted Output Cache for Tag {Tag}", tag);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Output Cache Eviction for {Tag}", tag);
}
}
}
}

View File

@ -142,6 +142,8 @@ namespace Oqtane
});
});
services.AddOutputCache();
services.AddMvc(options =>
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
@ -222,6 +224,7 @@ namespace Oqtane
app.UseJwtAuthorization();
app.UseRouting();
app.UseCors();
app.UseOutputCache();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();