Merge pull request #5158 from sbwalker/dev

rename Cache service to OutputCache
This commit is contained in:
Shaun Walker 2025-03-11 11:48:58 -04:00 committed by GitHub
commit 981add3872
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 127 additions and 67 deletions

View File

@ -53,7 +53,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddScoped<ISyncService, SyncService>();
services.AddScoped<ILocalizationCookieService, LocalizationCookieService>();
services.AddScoped<ICookieConsentService, CookieConsentService>();
services.AddScoped<ICacheService, CacheService>();
services.AddScoped<IOutputCacheService, OutputCacheService>();
// providers
services.AddScoped<ITextEditor, Oqtane.Modules.Controls.QuillJSTextEditor>();

View File

@ -14,7 +14,7 @@
@inject IStringLocalizer<Index> Localizer
@inject INotificationService NotificationService
@inject IStringLocalizer<SharedResources> SharedLocalizer
@inject ICacheService CacheService
@inject IOutputCacheService CacheService
@if (_initialized)
{
@ -936,7 +936,7 @@
}
private async Task EvictSitemapOutputCache() {
await CacheService.EvictOutputCacheByTag(Constants.SitemapOutputCacheTag);
await CacheService.EvictByTag(Constants.SitemapOutputCacheTag);
AddModuleMessage(Localizer["Success.SiteMap.CacheEvicted"], MessageType.Success);
}
}

View File

@ -153,8 +153,10 @@
</div>
<br /><br />
<button type="button" class="btn btn-success" @onclick="SaveConfig">@SharedLocalizer["Save"]</button>&nbsp;
<a class="btn btn-primary" href="swagger/index.html" target="_new">@Localizer["Swagger"]</a>&nbsp;
<ActionDialog Header="Restart Application" Message="Are You Sure You Wish To Restart The Application?" Action="Restart Application" Security="SecurityAccessLevel.Host" Class="btn btn-danger" OnClick="@(async () => await RestartApplication())" ResourceKey="RestartApplication" />
<br /><br />
<a class="btn btn-secondary" href="swagger/index.html" target="_new">@Localizer["Swagger"]</a>&nbsp;
<a class="btn btn-secondary" href="api/endpoint" target="_new">@Localizer["Endpoints"]</a>
</TabPanel>
<TabPanel Name="Log" Heading="Log" ResourceKey="Log">
<div class="container">

View File

@ -445,6 +445,6 @@
<value>Clear Cache</value>
</data>
<data name="Success.SiteMap.CacheEvicted" xml:space="preserve">
<value>SiteMap Output Cache Evicted</value>
<value>Site Map Cache Cleared</value>
</data>
</root>

View File

@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Swagger" xml:space="preserve">
<value>Access Swagger UI</value>
<value>Swagger UI</value>
</data>
<data name="FrameworkVersion.HelpText" xml:space="preserve">
<value>Framework Version</value>
@ -306,4 +306,7 @@
<data name="CacheControl.HelpText" xml:space="preserve">
<value>Provide a Cache-Control directive for static assets. For example 'public, max-age=60' indicates that static assets should be cached for 60 seconds. A blank value indicates caching is not enabled.</value>
</data>
<data name="Endpoints" xml:space="preserve">
<value>API Endpoints</value>
</data>
</root>

View File

@ -1,23 +0,0 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Oqtane.Documentation;
using Oqtane.Shared;
namespace Oqtane.Services
{
/// <inheritdoc cref="ICacheService" />
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class CacheService : ServiceBase, ICacheService
{
public CacheService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string ApiUrl => CreateApiUrl("Cache");
public async Task EvictOutputCacheByTag(string tag, CancellationToken cancellationToken = default)
{
await DeleteAsync($"{ApiUrl}/outputCache/evictByTag/{tag}");
}
}
}

View File

@ -6,13 +6,13 @@ namespace Oqtane.Services
/// <summary>
/// Service to manage cache
/// </summary>
public interface ICacheService
public interface IOutputCacheService
{
/// <summary>
/// Evicts the output cache for a specific tag
/// </summary>
/// <param name="tag"></param>
/// <returns></returns>
Task EvictOutputCacheByTag(string tag, CancellationToken cancellationToken = default);
Task EvictByTag(string tag);
}
}

View File

@ -0,0 +1,23 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Oqtane.Documentation;
using Oqtane.Shared;
namespace Oqtane.Services
{
/// <inheritdoc cref="IOutputCacheService" />
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class OutputCacheService : ServiceBase, IOutputCacheService
{
public OutputCacheService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string ApiUrl => CreateApiUrl("OutputCache");
public async Task EvictByTag(string tag)
{
await DeleteAsync($"{ApiUrl}/{tag}");
}
}
}

View File

@ -1,31 +0,0 @@
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

@ -0,0 +1,56 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Oqtane.Models;
using Oqtane.Shared;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Routing;
namespace Oqtane.Controllers
{
[Route(ControllerRoutes.ApiRoute)]
public class EndpointController : Controller
{
private readonly IEnumerable<EndpointDataSource> _endpointSources;
public EndpointController(IEnumerable<EndpointDataSource> endpointSources)
{
_endpointSources = endpointSources;
}
// GET api/<controller>
[HttpGet]
[Authorize(Roles = RoleNames.Host)]
public ActionResult Get()
{
var endpoints = _endpointSources
.SelectMany(item => item.Endpoints)
.OfType<RouteEndpoint>();
var output = endpoints.Select(
item =>
{
var controller = item.Metadata
.OfType<ControllerActionDescriptor>()
.FirstOrDefault();
var action = controller != null
? $"{controller.ControllerName}.{controller.ActionName}"
: null;
var controllerMethod = controller != null
? $"{controller.ControllerTypeInfo.FullName}:{controller.MethodInfo.Name}"
: null;
return new
{
Method = item.Metadata.OfType<HttpMethodMetadata>().FirstOrDefault()?.HttpMethods?[0],
Route = $"/{item.RoutePattern.RawText.TrimStart('/')}",
Action = action,
ControllerMethod = controllerMethod
};
}
);
return Json(output);
}
}
}

View File

@ -0,0 +1,30 @@
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 OutputCacheController : Controller
{
private readonly IOutputCacheService _cacheService;
public OutputCacheController(IOutputCacheService cacheService)
{
_cacheService = cacheService;
}
// DELETE api/<controller>/{tag}
[HttpDelete("{tag}")]
[Authorize(Roles = RoleNames.Admin)]
public async Task EvictByTag(string tag)
{
await _cacheService.EvictByTag(tag);
}
}
}

View File

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

View File

@ -12,24 +12,24 @@ using Oqtane.Shared;
namespace Oqtane.Services
{
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class ServerCacheService : ICacheService
public class ServerOutputCacheService : IOutputCacheService
{
private readonly IOutputCacheStore _outputCacheStore;
private readonly ILogManager _logger;
private readonly IHttpContextAccessor _accessor;
public ServerCacheService(IOutputCacheStore outputCacheStore, ILogManager logger, IHttpContextAccessor accessor)
public ServerOutputCacheService(IOutputCacheStore outputCacheStore, ILogManager logger, IHttpContextAccessor accessor)
{
_outputCacheStore = outputCacheStore;
_logger = logger;
_accessor = accessor;
}
public async Task EvictOutputCacheByTag(string tag, CancellationToken cancellationToken = default)
public async Task EvictByTag(string tag)
{
if (_accessor.HttpContext.User.IsInRole(RoleNames.Admin))
{
await _outputCacheStore.EvictByTagAsync(tag, cancellationToken);
await _outputCacheStore.EvictByTagAsync(tag, default);
_logger.Log(LogLevel.Information, this, LogFunction.Other, "Evicted Output Cache for Tag {Tag}", tag);
}
else