Merge pull request #6049 from sbwalker/dev

performance and scalability improvement - get the most recent HtmlText record directly from database and cache only a single object rather than the entire collection
This commit is contained in:
Shaun Walker
2026-02-17 16:25:35 -05:00
committed by GitHub
7 changed files with 50 additions and 119 deletions

View File

@@ -58,23 +58,6 @@ namespace Oqtane.Modules.HtmlText.Controllers
}
}
// GET api/<controller>/5/6
[HttpGet("{id}/{moduleId}")]
[Authorize(Policy = PolicyNames.ViewModule)]
public async Task<Models.HtmlText> Get(int id, int moduleId)
{
if (IsAuthorizedEntityId(EntityNames.Module, moduleId))
{
return await _htmlTextService.GetHtmlTextAsync(id, moduleId);
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Get Attempt {HtmlTextId}", id);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return null;
}
}
// POST api/<controller>
[HttpPost]
[Authorize(Policy = PolicyNames.EditModule)]

View File

@@ -1,18 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Infrastructure;
using Oqtane.Models;
using Oqtane.Modules.HtmlText.Repository;
using System.Net;
using Oqtane.Enums;
using Oqtane.Repository;
using Oqtane.Shared;
using Oqtane.Migrations.Framework;
using Oqtane.Documentation;
using Oqtane.Interfaces;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Oqtane.Documentation;
using Oqtane.Enums;
using Oqtane.Infrastructure;
using Oqtane.Interfaces;
using Oqtane.Migrations.Framework;
using Oqtane.Models;
using Oqtane.Modules.HtmlText.Repository;
using Oqtane.Repository;
using Oqtane.Shared;
// ReSharper disable ConvertToUsingDeclaration
@@ -21,20 +21,15 @@ namespace Oqtane.Modules.HtmlText.Manager
[PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, ISynchronizable, ISearchable
{
private readonly IHtmlTextRepository _htmlText;
private readonly IHtmlTextRepository _htmlTextRepository;
private readonly IDBContextDependencies _DBContextDependencies;
private readonly ISqlRepository _sqlRepository;
private readonly ITenantManager _tenantManager;
private readonly IMemoryCache _cache;
public HtmlTextManager(
IHtmlTextRepository htmlText,
IDBContextDependencies DBContextDependencies,
ISqlRepository sqlRepository,
ITenantManager tenantManager,
IMemoryCache cache)
public HtmlTextManager(IHtmlTextRepository htmlTextRepository, IDBContextDependencies DBContextDependencies, ISqlRepository sqlRepository, ITenantManager tenantManager, IMemoryCache cache)
{
_htmlText = htmlText;
_htmlTextRepository = htmlTextRepository;
_DBContextDependencies = DBContextDependencies;
_sqlRepository = sqlRepository;
_tenantManager = tenantManager;
@@ -61,7 +56,7 @@ namespace Oqtane.Modules.HtmlText.Manager
public string ExportModule(Module module)
{
string content = "";
var htmltext = GetModuleContent(module.ModuleId);
var htmltext = _htmlTextRepository.GetHtmlText(module.ModuleId);
if (htmltext != null)
{
content = WebUtility.HtmlEncode(htmltext.Content);
@@ -78,7 +73,7 @@ namespace Oqtane.Modules.HtmlText.Manager
public string ExtractModule(Module module, DateTime lastSynchronizedOn)
{
string content = "";
var htmltext = GetModuleContent(module.ModuleId);
var htmltext = _htmlTextRepository.GetHtmlText(module.ModuleId);
if (htmltext != null && htmltext.CreatedOn > lastSynchronizedOn)
{
content = WebUtility.HtmlEncode(htmltext.Content);
@@ -91,24 +86,13 @@ namespace Oqtane.Modules.HtmlText.Manager
SaveModuleContent(module, content);
}
private Models.HtmlText GetModuleContent(int moduleId)
{
// get the most recent htmltext record for the module
var htmltexts = _htmlText.GetHtmlTexts(moduleId);
if (htmltexts != null && htmltexts.Any())
{
return htmltexts.OrderByDescending(item => item.CreatedOn).First();
}
return null;
}
private void SaveModuleContent(Module module, string content)
{
content = WebUtility.HtmlDecode(content);
var htmlText = new Models.HtmlText();
htmlText.ModuleId = module.ModuleId;
htmlText.Content = content;
_htmlText.AddHtmlText(htmlText);
_htmlTextRepository.AddHtmlText(htmlText);
//clear the cache for the module
var alias = _tenantManager.GetAlias();
@@ -123,7 +107,7 @@ namespace Oqtane.Modules.HtmlText.Manager
{
var searchContents = new List<SearchContent>();
var htmltext = _htmlText.GetHtmlTexts(pageModule.ModuleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault();
var htmltext = _htmlTextRepository.GetHtmlText(pageModule.ModuleId);
if (htmltext != null && htmltext.CreatedOn >= lastIndexedOn)
{
searchContents.Add(new SearchContent

View File

@@ -11,7 +11,7 @@ namespace Oqtane.Modules.HtmlText.Repository
public interface IHtmlTextRepository
{
IEnumerable<Models.HtmlText> GetHtmlTexts(int moduleId);
Models.HtmlText GetHtmlText(int htmlTextId);
Models.HtmlText GetHtmlText(int moduleId);
Models.HtmlText AddHtmlText(Models.HtmlText htmlText);
void DeleteHtmlText(int htmlTextId);
}
@@ -34,17 +34,18 @@ namespace Oqtane.Modules.HtmlText.Repository
return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList();
}
public Models.HtmlText GetHtmlText(int htmlTextId)
public Models.HtmlText GetHtmlText(int moduleId)
{
using var db = _factory.CreateDbContext();
return db.HtmlText.Find(htmlTextId);
return db.HtmlText.Where(item => item.ModuleId == moduleId)?
.OrderByDescending(item => item.CreatedOn).FirstOrDefault();
}
public Models.HtmlText AddHtmlText(Models.HtmlText htmlText)
{
using var db = _factory.CreateDbContext();
var versions = int.Parse(_settingRepository.GetSettingValue(EntityNames.Module, htmlText.ModuleId, "Versions", "3"));
var versions = int.Parse(_settingRepository.GetSettingValue(EntityNames.Module, htmlText.ModuleId, "Versions", "5"));
if (versions > 0)
{
var htmlTexts = db.HtmlText.Where(item => item.ModuleId == htmlText.ModuleId).OrderByDescending(item => item.CreatedOn).ToList();

View File

@@ -17,16 +17,16 @@ namespace Oqtane.Modules.HtmlText.Services
[PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
public class ServerHtmlTextService : IHtmlTextService, ITransientService
{
private readonly IHtmlTextRepository _htmlText;
private readonly IHtmlTextRepository _htmlTextRepository;
private readonly IUserPermissions _userPermissions;
private readonly IMemoryCache _cache;
private readonly ILogManager _logger;
private readonly IHttpContextAccessor _accessor;
private readonly Alias _alias;
public ServerHtmlTextService(IHtmlTextRepository htmlText, IUserPermissions userPermissions, IMemoryCache cache, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
public ServerHtmlTextService(IHtmlTextRepository htmlTextRepository, IUserPermissions userPermissions, IMemoryCache cache, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
{
_htmlText = htmlText;
_htmlTextRepository = htmlTextRepository;
_userPermissions = userPermissions;
_cache = cache;
_logger = logger;
@@ -38,7 +38,7 @@ namespace Oqtane.Modules.HtmlText.Services
{
if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered))
{
return Task.FromResult(GetCachedHtmlTexts(moduleId));
return Task.FromResult(_htmlTextRepository.GetHtmlTexts(moduleId).ToList());
}
else
{
@@ -51,7 +51,11 @@ namespace Oqtane.Modules.HtmlText.Services
{
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View))
{
return Task.FromResult(GetCachedHtmlTexts(moduleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault());
return Task.FromResult(_cache.GetOrCreate($"HtmlText:{_alias.SiteKey}:{moduleId}", entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return _htmlTextRepository.GetHtmlText(moduleId);
}));
}
else
{
@@ -60,24 +64,11 @@ namespace Oqtane.Modules.HtmlText.Services
}
}
public Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId)
{
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View))
{
return Task.FromResult(GetCachedHtmlTexts(moduleId)?.FirstOrDefault(item => item.HtmlTextId == htmlTextId));
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Get Attempt {HtmlTextId} {ModuleId}", htmlTextId, moduleId);
return null;
}
}
public Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
{
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, htmlText.ModuleId, PermissionNames.Edit))
{
htmlText = _htmlText.AddHtmlText(htmlText);
htmlText = _htmlTextRepository.AddHtmlText(htmlText);
ClearCache(htmlText.ModuleId);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Html/Text Added {HtmlText}", htmlText);
}
@@ -93,7 +84,7 @@ namespace Oqtane.Modules.HtmlText.Services
{
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.Edit))
{
_htmlText.DeleteHtmlText(htmlTextId);
_htmlTextRepository.DeleteHtmlText(htmlTextId);
ClearCache(moduleId);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId);
}
@@ -104,15 +95,6 @@ namespace Oqtane.Modules.HtmlText.Services
return Task.CompletedTask;
}
private List<Models.HtmlText> GetCachedHtmlTexts(int moduleId)
{
return _cache.GetOrCreate($"HtmlText:{_alias.SiteKey}:{moduleId}", entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return _htmlText.GetHtmlTexts(moduleId).ToList();
});
}
private void ClearCache(int moduleId)
{
_cache.Remove($"HtmlText:{_alias.SiteKey}:{moduleId}");