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:
sbwalker
2026-02-17 16:25:10 -05:00
parent 912ed66547
commit df7f3f7bba
7 changed files with 50 additions and 119 deletions

View File

@@ -121,15 +121,11 @@
private async Task View(Models.HtmlText htmltext) private async Task View(Models.HtmlText htmltext)
{ {
try try
{
htmltext = await HtmlTextService.GetHtmlTextAsync(htmltext.HtmlTextId, htmltext.ModuleId);
if (htmltext != null)
{ {
_view = htmltext.Content; _view = htmltext.Content;
_view = Utilities.FormatContent(_view, PageState.Alias, "render"); _view = Utilities.FormatContent(_view, PageState.Alias, "render");
StateHasChanged(); StateHasChanged();
} }
}
catch (Exception ex) catch (Exception ex)
{ {
await logger.LogError(ex, "Error Viewing Content {Error}", ex.Message); await logger.LogError(ex, "Error Viewing Content {Error}", ex.Message);
@@ -140,9 +136,6 @@
private async Task Restore(Models.HtmlText htmltext) private async Task Restore(Models.HtmlText htmltext)
{ {
try try
{
htmltext = await HtmlTextService.GetHtmlTextAsync(htmltext.HtmlTextId, ModuleState.ModuleId);
if (htmltext != null)
{ {
var content = htmltext.Content; var content = htmltext.Content;
htmltext = new HtmlText(); htmltext = new HtmlText();
@@ -154,7 +147,6 @@
await LoadContent(); await LoadContent();
StateHasChanged(); StateHasChanged();
} }
}
catch (Exception ex) catch (Exception ex)
{ {
await logger.LogError(ex, "Error Restoring Content {Error}", ex.Message); await logger.LogError(ex, "Error Restoring Content {Error}", ex.Message);
@@ -165,9 +157,6 @@
private async Task Delete(Models.HtmlText htmltext) private async Task Delete(Models.HtmlText htmltext)
{ {
try try
{
htmltext = await HtmlTextService.GetHtmlTextAsync(htmltext.HtmlTextId, ModuleState.ModuleId);
if (htmltext != null)
{ {
await HtmlTextService.DeleteHtmlTextAsync(htmltext.HtmlTextId, htmltext.ModuleId); await HtmlTextService.DeleteHtmlTextAsync(htmltext.HtmlTextId, htmltext.ModuleId);
await logger.LogInformation("Content Deleted {HtmlText}", htmltext); await logger.LogInformation("Content Deleted {HtmlText}", htmltext);
@@ -175,7 +164,6 @@
await LoadContent(); await LoadContent();
StateHasChanged(); StateHasChanged();
} }
}
catch (Exception ex) catch (Exception ex)
{ {
await logger.LogError(ex, "Error Deleting Content {Error}", ex.Message); await logger.LogError(ex, "Error Deleting Content {Error}", ex.Message);

View File

@@ -14,8 +14,6 @@ namespace Oqtane.Modules.HtmlText.Services
Task<Models.HtmlText> GetHtmlTextAsync(int moduleId); Task<Models.HtmlText> GetHtmlTextAsync(int moduleId);
Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId);
Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmltext); Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmltext);
Task DeleteHtmlTextAsync(int htmlTextId, int moduleId); Task DeleteHtmlTextAsync(int htmlTextId, int moduleId);
@@ -38,11 +36,6 @@ namespace Oqtane.Modules.HtmlText.Services
return await GetJsonAsync<Models.HtmlText>(CreateAuthorizationPolicyUrl($"{ApiUrl}/{moduleId}", EntityNames.Module, moduleId)); return await GetJsonAsync<Models.HtmlText>(CreateAuthorizationPolicyUrl($"{ApiUrl}/{moduleId}", EntityNames.Module, moduleId));
} }
public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId)
{
return await GetJsonAsync<Models.HtmlText>(CreateAuthorizationPolicyUrl($"{ApiUrl}/{htmlTextId}/{moduleId}", EntityNames.Module, moduleId));
}
public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText) public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
{ {
return await PostJsonAsync(CreateAuthorizationPolicyUrl($"{ApiUrl}", EntityNames.Module, htmlText.ModuleId), htmlText); return await PostJsonAsync(CreateAuthorizationPolicyUrl($"{ApiUrl}", EntityNames.Module, htmlText.ModuleId), htmlText);

View File

@@ -39,7 +39,7 @@
try try
{ {
_dynamictokens = SettingService.GetSetting(ModuleState.Settings, "DynamicTokens", "false"); _dynamictokens = SettingService.GetSetting(ModuleState.Settings, "DynamicTokens", "false");
_versions = SettingService.GetSetting(ModuleState.Settings, "Versions", "3"); _versions = SettingService.GetSetting(ModuleState.Settings, "Versions", "5");
} }
catch (Exception ex) catch (Exception ex)
{ {

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> // POST api/<controller>
[HttpPost] [HttpPost]
[Authorize(Policy = PolicyNames.EditModule)] [Authorize(Policy = PolicyNames.EditModule)]

View File

@@ -1,18 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Oqtane.Infrastructure;
using Oqtane.Models;
using Oqtane.Modules.HtmlText.Repository;
using System.Net; 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 System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory; 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 // 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")] [PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, ISynchronizable, ISearchable public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, ISynchronizable, ISearchable
{ {
private readonly IHtmlTextRepository _htmlText; private readonly IHtmlTextRepository _htmlTextRepository;
private readonly IDBContextDependencies _DBContextDependencies; private readonly IDBContextDependencies _DBContextDependencies;
private readonly ISqlRepository _sqlRepository; private readonly ISqlRepository _sqlRepository;
private readonly ITenantManager _tenantManager; private readonly ITenantManager _tenantManager;
private readonly IMemoryCache _cache; private readonly IMemoryCache _cache;
public HtmlTextManager( public HtmlTextManager(IHtmlTextRepository htmlTextRepository, IDBContextDependencies DBContextDependencies, ISqlRepository sqlRepository, ITenantManager tenantManager, IMemoryCache cache)
IHtmlTextRepository htmlText,
IDBContextDependencies DBContextDependencies,
ISqlRepository sqlRepository,
ITenantManager tenantManager,
IMemoryCache cache)
{ {
_htmlText = htmlText; _htmlTextRepository = htmlTextRepository;
_DBContextDependencies = DBContextDependencies; _DBContextDependencies = DBContextDependencies;
_sqlRepository = sqlRepository; _sqlRepository = sqlRepository;
_tenantManager = tenantManager; _tenantManager = tenantManager;
@@ -61,7 +56,7 @@ namespace Oqtane.Modules.HtmlText.Manager
public string ExportModule(Module module) public string ExportModule(Module module)
{ {
string content = ""; string content = "";
var htmltext = GetModuleContent(module.ModuleId); var htmltext = _htmlTextRepository.GetHtmlText(module.ModuleId);
if (htmltext != null) if (htmltext != null)
{ {
content = WebUtility.HtmlEncode(htmltext.Content); content = WebUtility.HtmlEncode(htmltext.Content);
@@ -78,7 +73,7 @@ namespace Oqtane.Modules.HtmlText.Manager
public string ExtractModule(Module module, DateTime lastSynchronizedOn) public string ExtractModule(Module module, DateTime lastSynchronizedOn)
{ {
string content = ""; string content = "";
var htmltext = GetModuleContent(module.ModuleId); var htmltext = _htmlTextRepository.GetHtmlText(module.ModuleId);
if (htmltext != null && htmltext.CreatedOn > lastSynchronizedOn) if (htmltext != null && htmltext.CreatedOn > lastSynchronizedOn)
{ {
content = WebUtility.HtmlEncode(htmltext.Content); content = WebUtility.HtmlEncode(htmltext.Content);
@@ -91,24 +86,13 @@ namespace Oqtane.Modules.HtmlText.Manager
SaveModuleContent(module, content); 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) private void SaveModuleContent(Module module, string content)
{ {
content = WebUtility.HtmlDecode(content); content = WebUtility.HtmlDecode(content);
var htmlText = new Models.HtmlText(); var htmlText = new Models.HtmlText();
htmlText.ModuleId = module.ModuleId; htmlText.ModuleId = module.ModuleId;
htmlText.Content = content; htmlText.Content = content;
_htmlText.AddHtmlText(htmlText); _htmlTextRepository.AddHtmlText(htmlText);
//clear the cache for the module //clear the cache for the module
var alias = _tenantManager.GetAlias(); var alias = _tenantManager.GetAlias();
@@ -123,7 +107,7 @@ namespace Oqtane.Modules.HtmlText.Manager
{ {
var searchContents = new List<SearchContent>(); 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) if (htmltext != null && htmltext.CreatedOn >= lastIndexedOn)
{ {
searchContents.Add(new SearchContent searchContents.Add(new SearchContent

View File

@@ -11,7 +11,7 @@ namespace Oqtane.Modules.HtmlText.Repository
public interface IHtmlTextRepository public interface IHtmlTextRepository
{ {
IEnumerable<Models.HtmlText> GetHtmlTexts(int moduleId); IEnumerable<Models.HtmlText> GetHtmlTexts(int moduleId);
Models.HtmlText GetHtmlText(int htmlTextId); Models.HtmlText GetHtmlText(int moduleId);
Models.HtmlText AddHtmlText(Models.HtmlText htmlText); Models.HtmlText AddHtmlText(Models.HtmlText htmlText);
void DeleteHtmlText(int htmlTextId); void DeleteHtmlText(int htmlTextId);
} }
@@ -34,17 +34,18 @@ namespace Oqtane.Modules.HtmlText.Repository
return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList(); 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(); 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) public Models.HtmlText AddHtmlText(Models.HtmlText htmlText)
{ {
using var db = _factory.CreateDbContext(); 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) if (versions > 0)
{ {
var htmlTexts = db.HtmlText.Where(item => item.ModuleId == htmlText.ModuleId).OrderByDescending(item => item.CreatedOn).ToList(); 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")] [PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
public class ServerHtmlTextService : IHtmlTextService, ITransientService public class ServerHtmlTextService : IHtmlTextService, ITransientService
{ {
private readonly IHtmlTextRepository _htmlText; private readonly IHtmlTextRepository _htmlTextRepository;
private readonly IUserPermissions _userPermissions; private readonly IUserPermissions _userPermissions;
private readonly IMemoryCache _cache; private readonly IMemoryCache _cache;
private readonly ILogManager _logger; private readonly ILogManager _logger;
private readonly IHttpContextAccessor _accessor; private readonly IHttpContextAccessor _accessor;
private readonly Alias _alias; 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; _userPermissions = userPermissions;
_cache = cache; _cache = cache;
_logger = logger; _logger = logger;
@@ -38,7 +38,7 @@ namespace Oqtane.Modules.HtmlText.Services
{ {
if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered)) if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered))
{ {
return Task.FromResult(GetCachedHtmlTexts(moduleId)); return Task.FromResult(_htmlTextRepository.GetHtmlTexts(moduleId).ToList());
} }
else else
{ {
@@ -51,7 +51,11 @@ namespace Oqtane.Modules.HtmlText.Services
{ {
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View)) 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 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) public Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
{ {
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, htmlText.ModuleId, PermissionNames.Edit)) 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); ClearCache(htmlText.ModuleId);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Html/Text Added {HtmlText}", htmlText); _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)) if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.Edit))
{ {
_htmlText.DeleteHtmlText(htmlTextId); _htmlTextRepository.DeleteHtmlText(htmlTextId);
ClearCache(moduleId); ClearCache(moduleId);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId);
} }
@@ -104,15 +95,6 @@ namespace Oqtane.Modules.HtmlText.Services
return Task.CompletedTask; 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) private void ClearCache(int moduleId)
{ {
_cache.Remove($"HtmlText:{_alias.SiteKey}:{moduleId}"); _cache.Remove($"HtmlText:{_alias.SiteKey}:{moduleId}");