move HtmlText caching from repository to service layer
This commit is contained in:
@ -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")]
|
[PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")]
|
||||||
public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, ISearchable
|
public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, ISearchable
|
||||||
{
|
{
|
||||||
private readonly IServiceProvider _serviceProvider;
|
|
||||||
private readonly IHtmlTextRepository _htmlText;
|
private readonly IHtmlTextRepository _htmlText;
|
||||||
private readonly IDBContextDependencies _DBContextDependencies;
|
private readonly IDBContextDependencies _DBContextDependencies;
|
||||||
private readonly ISqlRepository _sqlRepository;
|
private readonly ISqlRepository _sqlRepository;
|
||||||
|
|
||||||
public HtmlTextManager(
|
public HtmlTextManager(
|
||||||
IServiceProvider serviceProvider,
|
|
||||||
IHtmlTextRepository htmlText,
|
IHtmlTextRepository htmlText,
|
||||||
IDBContextDependencies DBContextDependencies,
|
IDBContextDependencies DBContextDependencies,
|
||||||
ISqlRepository sqlRepository)
|
ISqlRepository sqlRepository)
|
||||||
{
|
{
|
||||||
_serviceProvider = serviceProvider;
|
|
||||||
_htmlText = htmlText;
|
_htmlText = htmlText;
|
||||||
_DBContextDependencies = DBContextDependencies;
|
_DBContextDependencies = DBContextDependencies;
|
||||||
_sqlRepository = sqlRepository;
|
_sqlRepository = sqlRepository;
|
||||||
@ -53,11 +50,8 @@ namespace Oqtane.Modules.HtmlText.Manager
|
|||||||
{
|
{
|
||||||
var searchContents = new List<SearchContent>();
|
var searchContents = new List<SearchContent>();
|
||||||
|
|
||||||
var htmltexts = _htmlText.GetHtmlTexts(pageModule.ModuleId);
|
var htmltext = _htmlText.GetHtmlTexts(pageModule.ModuleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault();
|
||||||
if (htmltexts != null && htmltexts.Any())
|
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
|
||||||
{
|
{
|
||||||
@ -66,7 +60,6 @@ namespace Oqtane.Modules.HtmlText.Manager
|
|||||||
ContentModifiedOn = htmltext.CreatedOn
|
ContentModifiedOn = htmltext.CreatedOn
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return Task.FromResult(searchContents);
|
return Task.FromResult(searchContents);
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Oqtane.Documentation;
|
using Oqtane.Documentation;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using System;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Oqtane.Shared;
|
|
||||||
|
|
||||||
namespace Oqtane.Modules.HtmlText.Repository
|
namespace Oqtane.Modules.HtmlText.Repository
|
||||||
{
|
{
|
||||||
@ -13,24 +9,16 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
public class HtmlTextRepository : IHtmlTextRepository, ITransientService
|
||||||
{
|
{
|
||||||
private readonly IDbContextFactory<HtmlTextContext> _factory;
|
private readonly IDbContextFactory<HtmlTextContext> _factory;
|
||||||
private readonly IMemoryCache _cache;
|
|
||||||
private readonly SiteState _siteState;
|
|
||||||
|
|
||||||
public HtmlTextRepository(IDbContextFactory<HtmlTextContext> factory, IMemoryCache cache, SiteState siteState)
|
public HtmlTextRepository(IDbContextFactory<HtmlTextContext> factory)
|
||||||
{
|
{
|
||||||
_factory = factory;
|
_factory = factory;
|
||||||
_cache = cache;
|
|
||||||
_siteState = siteState;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Models.HtmlText> GetHtmlTexts(int moduleId)
|
public IEnumerable<Models.HtmlText> GetHtmlTexts(int moduleId)
|
||||||
{
|
{
|
||||||
return _cache.GetOrCreate($"HtmlText:{_siteState.Alias.SiteKey}:{moduleId}", entry =>
|
|
||||||
{
|
|
||||||
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
|
||||||
using var db = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
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 htmlTextId)
|
||||||
@ -44,7 +32,6 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
using var db = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
db.HtmlText.Add(htmlText);
|
db.HtmlText.Add(htmlText);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
ClearCache(htmlText.ModuleId);
|
|
||||||
return htmlText;
|
return htmlText;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,47 +40,7 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
using var db = _factory.CreateDbContext();
|
using var db = _factory.CreateDbContext();
|
||||||
Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
Models.HtmlText htmlText = db.HtmlText.FirstOrDefault(item => item.HtmlTextId == htmlTextId);
|
||||||
if (htmlText != null) db.HtmlText.Remove(htmlText);
|
if (htmlText != null) db.HtmlText.Remove(htmlText);
|
||||||
ClearCache(htmlText.ModuleId);
|
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Models.HtmlText>> 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<Models.HtmlText> GetHtmlTextAsync(int htmlTextId)
|
|
||||||
{
|
|
||||||
using var db = _factory.CreateDbContext();
|
|
||||||
return await db.HtmlText.FindAsync(htmlTextId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Models.HtmlText> 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}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Oqtane.Documentation;
|
using Oqtane.Documentation;
|
||||||
|
|
||||||
namespace Oqtane.Modules.HtmlText.Repository
|
namespace Oqtane.Modules.HtmlText.Repository
|
||||||
@ -11,10 +10,5 @@ namespace Oqtane.Modules.HtmlText.Repository
|
|||||||
Models.HtmlText GetHtmlText(int htmlTextId);
|
Models.HtmlText GetHtmlText(int htmlTextId);
|
||||||
Models.HtmlText AddHtmlText(Models.HtmlText htmlText);
|
Models.HtmlText AddHtmlText(Models.HtmlText htmlText);
|
||||||
void DeleteHtmlText(int htmlTextId);
|
void DeleteHtmlText(int htmlTextId);
|
||||||
|
|
||||||
Task<IEnumerable<Models.HtmlText>> GetHtmlTextsAsync(int moduleId);
|
|
||||||
Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId);
|
|
||||||
Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText);
|
|
||||||
Task DeleteHtmlTextAsync(int htmlTextId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Oqtane.Documentation;
|
using Oqtane.Documentation;
|
||||||
using Oqtane.Enums;
|
using Oqtane.Enums;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
@ -17,24 +19,26 @@ namespace Oqtane.Modules.HtmlText.Services
|
|||||||
{
|
{
|
||||||
private readonly IHtmlTextRepository _htmlText;
|
private readonly IHtmlTextRepository _htmlText;
|
||||||
private readonly IUserPermissions _userPermissions;
|
private readonly IUserPermissions _userPermissions;
|
||||||
|
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, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
|
public ServerHtmlTextService(IHtmlTextRepository htmlText, IUserPermissions userPermissions, IMemoryCache cache, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
|
||||||
{
|
{
|
||||||
_htmlText = htmlText;
|
_htmlText = htmlText;
|
||||||
_userPermissions = userPermissions;
|
_userPermissions = userPermissions;
|
||||||
|
_cache = cache;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_accessor = accessor;
|
_accessor = accessor;
|
||||||
_alias = tenantManager.GetAlias();
|
_alias = tenantManager.GetAlias();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Models.HtmlText>> GetHtmlTextsAsync(int moduleId)
|
public Task<List<Models.HtmlText>> GetHtmlTextsAsync(int moduleId)
|
||||||
{
|
{
|
||||||
if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered))
|
if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered))
|
||||||
{
|
{
|
||||||
return (await _htmlText.GetHtmlTextsAsync(moduleId)).ToList();
|
return Task.FromResult(GetCachedHtmlTexts(moduleId));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -43,19 +47,11 @@ namespace Oqtane.Modules.HtmlText.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Models.HtmlText> GetHtmlTextAsync(int moduleId)
|
public Task<Models.HtmlText> GetHtmlTextAsync(int moduleId)
|
||||||
{
|
{
|
||||||
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))
|
||||||
{
|
{
|
||||||
var htmltexts = await _htmlText.GetHtmlTextsAsync(moduleId);
|
return Task.FromResult(GetCachedHtmlTexts(moduleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault());
|
||||||
if (htmltexts != null && htmltexts.Any())
|
|
||||||
{
|
|
||||||
return htmltexts.OrderByDescending(item => item.CreatedOn).First();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -64,11 +60,11 @@ namespace Oqtane.Modules.HtmlText.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId)
|
public Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId)
|
||||||
{
|
{
|
||||||
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 await _htmlText.GetHtmlTextAsync(htmlTextId);
|
return Task.FromResult(GetCachedHtmlTexts(moduleId)?.FirstOrDefault(item => item.HtmlTextId == htmlTextId));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -77,11 +73,12 @@ namespace Oqtane.Modules.HtmlText.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async 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 = await _htmlText.AddHtmlTextAsync(htmlText);
|
htmlText = _htmlText.AddHtmlText(htmlText);
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -89,20 +86,36 @@ namespace Oqtane.Modules.HtmlText.Services
|
|||||||
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Add Attempt {HtmlText}", htmlText);
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Add Attempt {HtmlText}", htmlText);
|
||||||
htmlText = null;
|
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))
|
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);
|
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Delete Attempt {HtmlTextId} {ModuleId}", htmlTextId, moduleId);
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Delete Attempt {HtmlTextId} {ModuleId}", htmlTextId, moduleId);
|
||||||
}
|
}
|
||||||
|
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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user