Merge pull request #5132 from mdmontesinos/sitemap-cache
resolves #4899: output cache for sitemap
This commit is contained in:
31
Oqtane.Server/Controllers/CacheController.cs
Normal file
31
Oqtane.Server/Controllers/CacheController.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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>();
|
||||
|
@ -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;
|
||||
|
41
Oqtane.Server/Services/CacheService.cs
Normal file
41
Oqtane.Server/Services/CacheService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
Reference in New Issue
Block a user