From 57ef4c0396f5dd0ae0d67794f2743dc5f483e471 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Wed, 14 Aug 2024 10:00:56 -0400 Subject: [PATCH] move HtmlText caching from repository to service layer --- .../HtmlText/Manager/HtmlTextManager.cs | 21 +++---- .../HtmlText/Repository/HtmlTextRepository.cs | 59 +------------------ .../Repository/IHtmlTextRepository.cs | 6 -- .../HtmlText/Services/HtmlTextService.cs | 53 ++++++++++------- 4 files changed, 43 insertions(+), 96 deletions(-) diff --git a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs index fac72001..bca9f928 100644 --- a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs +++ b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs @@ -20,18 +20,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, ISearchable { - private readonly IServiceProvider _serviceProvider; private readonly IHtmlTextRepository _htmlText; private readonly IDBContextDependencies _DBContextDependencies; private readonly ISqlRepository _sqlRepository; public HtmlTextManager( - IServiceProvider serviceProvider, IHtmlTextRepository htmlText, IDBContextDependencies DBContextDependencies, ISqlRepository sqlRepository) { - _serviceProvider = serviceProvider; _htmlText = htmlText; _DBContextDependencies = DBContextDependencies; _sqlRepository = sqlRepository; @@ -53,19 +50,15 @@ namespace Oqtane.Modules.HtmlText.Manager { var searchContents = new List(); - var htmltexts = _htmlText.GetHtmlTexts(pageModule.ModuleId); - if (htmltexts != null && htmltexts.Any()) + var htmltext = _htmlText.GetHtmlTexts(pageModule.ModuleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault(); + if (htmltext != null && htmltext.CreatedOn >= lastIndexedOn) { - var htmltext = htmltexts.OrderByDescending(item => item.CreatedOn).First(); - if (htmltext.CreatedOn >= lastIndexedOn) + searchContents.Add(new SearchContent { - searchContents.Add(new SearchContent - { - Body = htmltext.Content, - ContentModifiedBy = htmltext.CreatedBy, - ContentModifiedOn = htmltext.CreatedOn - }); - } + Body = htmltext.Content, + ContentModifiedBy = htmltext.CreatedBy, + ContentModifiedOn = htmltext.CreatedOn + }); } return Task.FromResult(searchContents); diff --git a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs index 8d6de983..cfeb8eb3 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs @@ -1,11 +1,7 @@ using System.Linq; using Oqtane.Documentation; using System.Collections.Generic; -using Microsoft.Extensions.Caching.Memory; -using System; using Microsoft.EntityFrameworkCore; -using System.Threading.Tasks; -using Oqtane.Shared; namespace Oqtane.Modules.HtmlText.Repository { @@ -13,24 +9,16 @@ namespace Oqtane.Modules.HtmlText.Repository public class HtmlTextRepository : IHtmlTextRepository, ITransientService { private readonly IDbContextFactory _factory; - private readonly IMemoryCache _cache; - private readonly SiteState _siteState; - public HtmlTextRepository(IDbContextFactory factory, IMemoryCache cache, SiteState siteState) + public HtmlTextRepository(IDbContextFactory factory) { _factory = factory; - _cache = cache; - _siteState = siteState; } public IEnumerable GetHtmlTexts(int moduleId) { - return _cache.GetOrCreate($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", entry => - { - entry.SlidingExpiration = TimeSpan.FromMinutes(30); - using var db = _factory.CreateDbContext(); - return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList(); - }); + using var db = _factory.CreateDbContext(); + return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList(); } public Models.HtmlText GetHtmlText(int htmlTextId) @@ -44,7 +32,6 @@ namespace Oqtane.Modules.HtmlText.Repository using var db = _factory.CreateDbContext(); db.HtmlText.Add(htmlText); db.SaveChanges(); - ClearCache(htmlText.ModuleId); return htmlText; } @@ -53,47 +40,7 @@ namespace Oqtane.Modules.HtmlText.Repository using var db = _factory.CreateDbContext(); Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); if (htmlText != null) db.HtmlText.Remove(htmlText); - ClearCache(htmlText.ModuleId); db.SaveChanges(); } - - public async Task> GetHtmlTextsAsync(int moduleId) - { - return await _cache.GetOrCreateAsync($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", async entry => - { - entry.SlidingExpiration = TimeSpan.FromMinutes(30); - using var db = _factory.CreateDbContext(); - return await db.HtmlText.Where(item => item.ModuleId == moduleId).ToListAsync(); - }); - } - - public async Task GetHtmlTextAsync(int htmlTextId) - { - using var db = _factory.CreateDbContext(); - return await db.HtmlText.FindAsync(htmlTextId); - } - - public async Task AddHtmlTextAsync(Models.HtmlText htmlText) - { - using var db = _factory.CreateDbContext(); - db.HtmlText.Add(htmlText); - await db.SaveChangesAsync(); - ClearCache(htmlText.ModuleId); - return htmlText; - } - - public async Task DeleteHtmlTextAsync(int htmlTextId) - { - using var db = _factory.CreateDbContext(); - Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId); - db.HtmlText.Remove(htmlText); - ClearCache(htmlText.ModuleId); - await db.SaveChangesAsync(); - } - - private void ClearCache(int moduleId) - { - _cache.Remove($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}"); - } } } diff --git a/Oqtane.Server/Modules/HtmlText/Repository/IHtmlTextRepository.cs b/Oqtane.Server/Modules/HtmlText/Repository/IHtmlTextRepository.cs index 8715ccc0..d586f0ef 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/IHtmlTextRepository.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/IHtmlTextRepository.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Threading.Tasks; using Oqtane.Documentation; namespace Oqtane.Modules.HtmlText.Repository @@ -11,10 +10,5 @@ namespace Oqtane.Modules.HtmlText.Repository Models.HtmlText GetHtmlText(int htmlTextId); Models.HtmlText AddHtmlText(Models.HtmlText htmlText); void DeleteHtmlText(int htmlTextId); - - Task> GetHtmlTextsAsync(int moduleId); - Task GetHtmlTextAsync(int htmlTextId); - Task AddHtmlTextAsync(Models.HtmlText htmlText); - Task DeleteHtmlTextAsync(int htmlTextId); } } diff --git a/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs b/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs index 1b551d0a..49055c4e 100644 --- a/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs +++ b/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs @@ -1,7 +1,9 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Caching.Memory; using Oqtane.Documentation; using Oqtane.Enums; using Oqtane.Infrastructure; @@ -17,24 +19,26 @@ namespace Oqtane.Modules.HtmlText.Services { private readonly IHtmlTextRepository _htmlText; 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, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor) + public ServerHtmlTextService(IHtmlTextRepository htmlText, IUserPermissions userPermissions, IMemoryCache cache, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor) { _htmlText = htmlText; _userPermissions = userPermissions; + _cache = cache; _logger = logger; _accessor = accessor; _alias = tenantManager.GetAlias(); } - public async Task> GetHtmlTextsAsync(int moduleId) + public Task> GetHtmlTextsAsync(int moduleId) { if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered)) { - return (await _htmlText.GetHtmlTextsAsync(moduleId)).ToList(); + return Task.FromResult(GetCachedHtmlTexts(moduleId)); } else { @@ -43,19 +47,11 @@ namespace Oqtane.Modules.HtmlText.Services } } - public async Task GetHtmlTextAsync(int moduleId) + public Task GetHtmlTextAsync(int moduleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View)) { - var htmltexts = await _htmlText.GetHtmlTextsAsync(moduleId); - if (htmltexts != null && htmltexts.Any()) - { - return htmltexts.OrderByDescending(item => item.CreatedOn).First(); - } - else - { - return null; - } + return Task.FromResult(GetCachedHtmlTexts(moduleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault()); } else { @@ -64,11 +60,11 @@ namespace Oqtane.Modules.HtmlText.Services } } - public async Task GetHtmlTextAsync(int htmlTextId, int moduleId) + public Task GetHtmlTextAsync(int htmlTextId, int moduleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View)) { - return await _htmlText.GetHtmlTextAsync(htmlTextId); + return Task.FromResult(GetCachedHtmlTexts(moduleId)?.FirstOrDefault(item => item.HtmlTextId == htmlTextId)); } else { @@ -77,11 +73,12 @@ namespace Oqtane.Modules.HtmlText.Services } } - public async Task AddHtmlTextAsync(Models.HtmlText htmlText) + public Task AddHtmlTextAsync(Models.HtmlText htmlText) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, htmlText.ModuleId, PermissionNames.Edit)) { - htmlText = await _htmlText.AddHtmlTextAsync(htmlText); + htmlText = _htmlText.AddHtmlText(htmlText); + ClearCache(htmlText.ModuleId); _logger.Log(LogLevel.Information, this, LogFunction.Create, "Html/Text Added {HtmlText}", htmlText); } else @@ -89,20 +86,36 @@ namespace Oqtane.Modules.HtmlText.Services _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Add Attempt {HtmlText}", htmlText); htmlText = null; } - return htmlText; + return Task.FromResult(htmlText); } - public async Task DeleteHtmlTextAsync(int htmlTextId, int moduleId) + public Task DeleteHtmlTextAsync(int htmlTextId, int moduleId) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.Edit)) { - await _htmlText.DeleteHtmlTextAsync(htmlTextId); + _htmlText.DeleteHtmlText(htmlTextId); + ClearCache(moduleId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId); } else { _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Delete Attempt {HtmlTextId} {ModuleId}", htmlTextId, moduleId); } + return Task.CompletedTask; + } + + private List 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}"); } } }