News Dashboard: Adding functionality to overwrite the cache duration (#21064)

* Adding functionality to overwrite the cacheduration for NewsDashboard

* Making the extension its own class, as to avoid having to inherit the entire service.

* Changing options to duration and adding interface
This commit is contained in:
Nicklas Kramer
2025-12-05 10:43:43 +01:00
committed by GitHub
parent 5683ae9e4b
commit 15b2cb7bd1
4 changed files with 47 additions and 2 deletions

View File

@@ -8,8 +8,10 @@ internal static class NewsDashboardBuilderExtensions
{ {
internal static IUmbracoBuilder AddNewsDashboard(this IUmbracoBuilder builder) internal static IUmbracoBuilder AddNewsDashboard(this IUmbracoBuilder builder)
{ {
builder.Services.AddSingleton<INewsCacheDurationProvider, NewsCacheDurationProvider>();
builder.Services.AddSingleton<INewsDashboardService, NewsDashboardService>(); builder.Services.AddSingleton<INewsDashboardService, NewsDashboardService>();
return builder; return builder;
} }
} }

View File

@@ -0,0 +1,12 @@
namespace Umbraco.Cms.Api.Management.Services.NewsDashboard;
/// <summary>
/// Cache duration provider for the news dashboard service.
/// </summary>
public interface INewsCacheDurationProvider
{
/// <summary>
/// Gets the cache duration for news dashboard items.
/// </summary>
TimeSpan CacheDuration { get; }
}

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Cms.Api.Management.Services.NewsDashboard;
public class NewsCacheDurationProvider : INewsCacheDurationProvider
{
/// <inheritdoc />
public TimeSpan CacheDuration => TimeSpan.FromMinutes(30);
}

View File

@@ -1,11 +1,13 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Umbraco.Cms.Api.Management.ViewModels.NewsDashboard; using Umbraco.Cms.Api.Management.ViewModels.NewsDashboard;
using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Configuration; using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Telemetry; using Umbraco.Cms.Core.Telemetry;
using Umbraco.Extensions; using Umbraco.Extensions;
@@ -21,6 +23,7 @@ public class NewsDashboardService : INewsDashboardService
private readonly ILogger<NewsDashboardService> _logger; private readonly ILogger<NewsDashboardService> _logger;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
private readonly INewsCacheDurationProvider _newsCacheDurationProvider;
private static readonly HttpClient _httpClient = new(); private static readonly HttpClient _httpClient = new();
@@ -33,7 +36,8 @@ public class NewsDashboardService : INewsDashboardService
ISiteIdentifierService siteIdentifierService, ISiteIdentifierService siteIdentifierService,
ILogger<NewsDashboardService> logger, ILogger<NewsDashboardService> logger,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor, IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IOptions<GlobalSettings> globalSettings) IOptions<GlobalSettings> globalSettings,
INewsCacheDurationProvider newsCacheDurationProvider)
{ {
_appCaches = appCaches; _appCaches = appCaches;
_umbracoVersion = umbracoVersion; _umbracoVersion = umbracoVersion;
@@ -41,6 +45,26 @@ public class NewsDashboardService : INewsDashboardService
_logger = logger; _logger = logger;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor; _backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_globalSettings = globalSettings.Value; _globalSettings = globalSettings.Value;
_newsCacheDurationProvider = newsCacheDurationProvider;
}
[Obsolete("Please use the constructor taking all parameters. Scheduled for removal in Umbraco 19")]
public NewsDashboardService(
AppCaches appCaches,
IUmbracoVersion umbracoVersion,
ISiteIdentifierService siteIdentifierService,
ILogger<NewsDashboardService> logger,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IOptions<GlobalSettings> globalSettings)
: this(
appCaches,
umbracoVersion,
siteIdentifierService,
logger,
backOfficeSecurityAccessor,
globalSettings,
StaticServiceProvider.Instance.GetRequiredService<INewsCacheDurationProvider>())
{
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -69,7 +93,7 @@ public class NewsDashboardService : INewsDashboardService
if (TryMapModel(json, out NewsDashboardResponseModel? model)) if (TryMapModel(json, out NewsDashboardResponseModel? model))
{ {
_appCaches.RuntimeCache.InsertCacheItem(CacheKey, () => model, new TimeSpan(0, 30, 0)); _appCaches.RuntimeCache.InsertCacheItem(CacheKey, () => model, _newsCacheDurationProvider.CacheDuration);
content = model; content = model;
} }
} }