v10: Improve redirect content finder scalability (#12341)

* Async support for content finders. Improve log performance.

* Improve redirect contentfinder scalablilty with async calls to the database
This commit is contained in:
Chad
2022-05-04 07:35:33 +12:00
committed by GitHub
parent 0561d4b5a1
commit ae54cb9947
5 changed files with 95 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Persistence.Repositories
@@ -42,6 +43,13 @@ namespace Umbraco.Cms.Core.Persistence.Repositories
/// <returns>The most recent redirect URL corresponding to the route.</returns>
IRedirectUrl? GetMostRecentUrl(string url);
/// <summary>
/// Gets the most recent redirect URL corresponding to an Umbraco redirect URL route.
/// </summary>
/// <param name="url">The Umbraco redirect URL route.</param>
/// <returns>The most recent redirect URL corresponding to the route.</returns>
Task<IRedirectUrl?> GetMostRecentUrlAsync(string url);
/// <summary>
/// Gets the most recent redirect URL corresponding to an Umbraco redirect URL route.
/// </summary>
@@ -50,6 +58,14 @@ namespace Umbraco.Cms.Core.Persistence.Repositories
/// <returns>The most recent redirect URL corresponding to the route.</returns>
IRedirectUrl? GetMostRecentUrl(string url, string culture);
/// <summary>
/// Gets the most recent redirect URL corresponding to an Umbraco redirect URL route.
/// </summary>
/// <param name="url">The Umbraco redirect URL route.</param>
/// <param name="culture">The culture the domain is associated with</param>
/// <returns>The most recent redirect URL corresponding to the route.</returns>
Task<IRedirectUrl?> GetMostRecentUrlAsync(string url, string culture);
/// <summary>
/// Gets all redirect URLs for a content item.
/// </summary>

View File

@@ -55,7 +55,7 @@ namespace Umbraco.Cms.Core.Routing
? frequest.Domain.ContentId + DomainUtilities.PathRelativeToDomain(frequest.Domain.Uri, frequest.AbsolutePathDecoded)
: frequest.AbsolutePathDecoded;
IRedirectUrl? redirectUrl = _redirectUrlService.GetMostRecentRedirectUrl(route, frequest.Culture);
IRedirectUrl? redirectUrl = await _redirectUrlService.GetMostRecentRedirectUrlAsync(route, frequest.Culture);
if (redirectUrl == null)
{

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Core.Services
@@ -56,6 +57,14 @@ namespace Umbraco.Cms.Core.Services
/// <returns>The most recent redirect URLs corresponding to the route.</returns>
IRedirectUrl? GetMostRecentRedirectUrl(string url, string? culture);
/// <summary>
/// Gets the most recent redirect URLs corresponding to an Umbraco redirect URL route.
/// </summary>
/// <param name="url">The Umbraco redirect URL route.</param>
/// <param name="culture">The culture of the request.</param>
/// <returns>The most recent redirect URLs corresponding to the route.</returns>
Task<IRedirectUrl?> GetMostRecentRedirectUrlAsync(string url, string? culture);
/// <summary>
/// Gets all redirect URLs for a content item.
/// </summary>

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
@@ -76,6 +77,13 @@ namespace Umbraco.Cms.Core.Services
return _redirectUrlRepository.GetMostRecentUrl(url);
}
}
public async Task<IRedirectUrl?> GetMostRecentRedirectUrlAsync(string url)
{
using (var scope = ScopeProvider.CreateCoreScope(autoComplete: true))
{
return await _redirectUrlRepository.GetMostRecentUrlAsync(url);
}
}
public IEnumerable<IRedirectUrl> GetContentRedirectUrls(Guid contentKey)
{
@@ -118,5 +126,18 @@ namespace Umbraco.Cms.Core.Services
}
}
public async Task<IRedirectUrl?> GetMostRecentRedirectUrlAsync(string url, string? culture)
{
if (string.IsNullOrWhiteSpace(culture))
{
return await GetMostRecentRedirectUrlAsync(url);
}
using (var scope = ScopeProvider.CreateCoreScope(autoComplete: true))
{
return await _redirectUrlRepository.GetMostRecentUrlAsync(url, culture);
}
}
}
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NPoco;
using Umbraco.Cms.Core;
@@ -40,14 +41,27 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
public void Delete(Guid id) => Database.Delete<RedirectUrlDto>(id);
public IRedirectUrl? GetMostRecentUrl(string url)
{
Sql<ISqlContext> sql = GetMostRecentSql(url);
List<RedirectUrlDto> dtos = Database.Fetch<RedirectUrlDto>(sql);
RedirectUrlDto? dto = dtos.FirstOrDefault();
return dto == null ? null : Map(dto);
}
public async Task<IRedirectUrl?> GetMostRecentUrlAsync(string url)
{
Sql<ISqlContext> sql = GetMostRecentSql(url);
List<RedirectUrlDto> dtos = await Database.FetchAsync<RedirectUrlDto>(sql);
RedirectUrlDto? dto = dtos.FirstOrDefault();
return dto == null ? null : Map(dto);
}
private Sql<ISqlContext> GetMostRecentSql(string url)
{
var urlHash = url.GenerateHash<SHA1>();
Sql<ISqlContext> sql = GetBaseQuery(false)
.Where<RedirectUrlDto>(x => x.Url == url && x.UrlHash == urlHash)
.OrderByDescending<RedirectUrlDto>(x => x.CreateDateUtc);
List<RedirectUrlDto> dtos = Database.Fetch<RedirectUrlDto>(sql);
RedirectUrlDto? dto = dtos.FirstOrDefault();
return dto == null ? null : Map(dto);
return sql;
}
public IRedirectUrl? GetMostRecentUrl(string url, string culture)
@@ -57,12 +71,39 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
return GetMostRecentUrl(url);
}
Sql<ISqlContext> sql = GetMostRecentUrlSql(url, culture);
List<RedirectUrlDto> dtos = Database.Fetch<RedirectUrlDto>(sql);
RedirectUrlDto? dto = dtos.FirstOrDefault(f => f.Culture == culture.ToLower());
if (dto == null)
{
dto = dtos.FirstOrDefault(f => string.IsNullOrWhiteSpace(f.Culture));
}
return dto == null ? null : Map(dto);
}
private Sql<ISqlContext> GetMostRecentUrlSql(string url, string culture)
{
var urlHash = url.GenerateHash<SHA1>();
Sql<ISqlContext> sql = GetBaseQuery(false)
.Where<RedirectUrlDto>(x => x.Url == url && x.UrlHash == urlHash &&
(x.Culture == culture.ToLower() || x.Culture == null || x.Culture == string.Empty))
.OrderByDescending<RedirectUrlDto>(x => x.CreateDateUtc);
List<RedirectUrlDto> dtos = Database.Fetch<RedirectUrlDto>(sql);
return sql;
}
public async Task<IRedirectUrl?> GetMostRecentUrlAsync(string url, string culture)
{
if (string.IsNullOrWhiteSpace(culture))
{
return GetMostRecentUrl(url);
}
Sql<ISqlContext> sql = GetMostRecentUrlSql(url, culture);
List<RedirectUrlDto> dtos = await Database.FetchAsync<RedirectUrlDto>(sql);
RedirectUrlDto? dto = dtos.FirstOrDefault(f => f.Culture == culture.ToLower());
if (dto == null)