Skip to content

Commit 15b2cb7

Browse files
authored
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
1 parent 5683ae9 commit 15b2cb7

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/Umbraco.Cms.Api.Management/DependencyInjection/DashboardBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ internal static class NewsDashboardBuilderExtensions
88
{
99
internal static IUmbracoBuilder AddNewsDashboard(this IUmbracoBuilder builder)
1010
{
11+
builder.Services.AddSingleton<INewsCacheDurationProvider, NewsCacheDurationProvider>();
1112
builder.Services.AddSingleton<INewsDashboardService, NewsDashboardService>();
1213

1314
return builder;
1415
}
1516
}
17+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Umbraco.Cms.Api.Management.Services.NewsDashboard;
2+
3+
/// <summary>
4+
/// Cache duration provider for the news dashboard service.
5+
/// </summary>
6+
public interface INewsCacheDurationProvider
7+
{
8+
/// <summary>
9+
/// Gets the cache duration for news dashboard items.
10+
/// </summary>
11+
TimeSpan CacheDuration { get; }
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Umbraco.Cms.Api.Management.Services.NewsDashboard;
2+
3+
public class NewsCacheDurationProvider : INewsCacheDurationProvider
4+
{
5+
/// <inheritdoc />
6+
public TimeSpan CacheDuration => TimeSpan.FromMinutes(30);
7+
}

src/Umbraco.Cms.Api.Management/Services/NewsDashboard/NewsDashboardService.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Text.Json;
3+
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Logging;
45
using Microsoft.Extensions.Options;
56
using Umbraco.Cms.Api.Management.ViewModels.NewsDashboard;
67
using Umbraco.Cms.Core.Cache;
78
using Umbraco.Cms.Core.Configuration;
89
using Umbraco.Cms.Core.Configuration.Models;
10+
using Umbraco.Cms.Core.DependencyInjection;
911
using Umbraco.Cms.Core.Security;
1012
using Umbraco.Cms.Core.Telemetry;
1113
using Umbraco.Extensions;
@@ -21,6 +23,7 @@ public class NewsDashboardService : INewsDashboardService
2123
private readonly ILogger<NewsDashboardService> _logger;
2224
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
2325
private readonly GlobalSettings _globalSettings;
26+
private readonly INewsCacheDurationProvider _newsCacheDurationProvider;
2427

2528
private static readonly HttpClient _httpClient = new();
2629

@@ -33,14 +36,35 @@ public NewsDashboardService(
3336
ISiteIdentifierService siteIdentifierService,
3437
ILogger<NewsDashboardService> logger,
3538
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
36-
IOptions<GlobalSettings> globalSettings)
39+
IOptions<GlobalSettings> globalSettings,
40+
INewsCacheDurationProvider newsCacheDurationProvider)
3741
{
3842
_appCaches = appCaches;
3943
_umbracoVersion = umbracoVersion;
4044
_siteIdentifierService = siteIdentifierService;
4145
_logger = logger;
4246
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
4347
_globalSettings = globalSettings.Value;
48+
_newsCacheDurationProvider = newsCacheDurationProvider;
49+
}
50+
51+
[Obsolete("Please use the constructor taking all parameters. Scheduled for removal in Umbraco 19")]
52+
public NewsDashboardService(
53+
AppCaches appCaches,
54+
IUmbracoVersion umbracoVersion,
55+
ISiteIdentifierService siteIdentifierService,
56+
ILogger<NewsDashboardService> logger,
57+
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
58+
IOptions<GlobalSettings> globalSettings)
59+
: this(
60+
appCaches,
61+
umbracoVersion,
62+
siteIdentifierService,
63+
logger,
64+
backOfficeSecurityAccessor,
65+
globalSettings,
66+
StaticServiceProvider.Instance.GetRequiredService<INewsCacheDurationProvider>())
67+
{
4468
}
4569

4670
/// <inheritdoc />
@@ -69,7 +93,7 @@ public async Task<NewsDashboardResponseModel> GetItemsAsync()
6993

7094
if (TryMapModel(json, out NewsDashboardResponseModel? model))
7195
{
72-
_appCaches.RuntimeCache.InsertCacheItem(CacheKey, () => model, new TimeSpan(0, 30, 0));
96+
_appCaches.RuntimeCache.InsertCacheItem(CacheKey, () => model, _newsCacheDurationProvider.CacheDuration);
7397
content = model;
7498
}
7599
}

0 commit comments

Comments
 (0)