move HtmlText caching from repository to service layer

This commit is contained in:
sbwalker
2024-08-14 10:00:56 -04:00
parent 2fc6dbc222
commit 57ef4c0396
4 changed files with 43 additions and 96 deletions

View File

@ -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<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;
_cache = cache;
_siteState = siteState;
}
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();
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<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}");
}
}
}