From e7157a852860e602b8ce69c8889042f7ad7e74a9 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Tue, 27 Feb 2024 15:12:48 -0500 Subject: [PATCH] replace Task.Run() logic in SiteService with async SiteRepository methods --- Oqtane.Server/Extensions/CacheExtensions.cs | 46 ++++++++++++++----- .../Repository/Interfaces/ISiteRepository.cs | 7 +++ Oqtane.Server/Repository/SiteRepository.cs | 44 +++++++++++++++++- Oqtane.Server/Services/SiteService.cs | 33 ++++++------- 4 files changed, 99 insertions(+), 31 deletions(-) diff --git a/Oqtane.Server/Extensions/CacheExtensions.cs b/Oqtane.Server/Extensions/CacheExtensions.cs index 1aff73f55..21824d1cf 100644 --- a/Oqtane.Server/Extensions/CacheExtensions.cs +++ b/Oqtane.Server/Extensions/CacheExtensions.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; namespace Oqtane.Extensions { @@ -19,23 +21,45 @@ public static TItem GetOrCreate(this IMemoryCache cache, string key, Func if (track) { - // track the cache key - List cachekeys; - if (!cache.TryGetValue(_cachekeys, out cachekeys)) - { - cachekeys = new List(); - } - if (!cachekeys.Contains(key)) - { - cachekeys.Add(key); - cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove }); - } + TrackCacheKey(cache, key); } } return (TItem)result; } + public static async Task GetOrCreateAsync(this IMemoryCache cache, string key, Func> factory, bool track) + { + if (!cache.TryGetValue(key, out object result)) + { + using ICacheEntry entry = cache.CreateEntry(key); + result = await factory(entry).ConfigureAwait(false); + entry.Value = result; + + if (track) + { + TrackCacheKey(cache, key); + } + } + + return (TItem)result; + } + + private static void TrackCacheKey(IMemoryCache cache, string key) + { + // track the cache key + List cachekeys; + if (!cache.TryGetValue(_cachekeys, out cachekeys)) + { + cachekeys = new List(); + } + if (!cachekeys.Contains(key)) + { + cachekeys.Add(key); + cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove }); + } + } + public static void Remove(this IMemoryCache cache, string key, bool track) { List cachekeys; diff --git a/Oqtane.Server/Repository/Interfaces/ISiteRepository.cs b/Oqtane.Server/Repository/Interfaces/ISiteRepository.cs index fbba81f9f..8dfebc04a 100644 --- a/Oqtane.Server/Repository/Interfaces/ISiteRepository.cs +++ b/Oqtane.Server/Repository/Interfaces/ISiteRepository.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; using Oqtane.Models; namespace Oqtane.Repository @@ -6,11 +7,17 @@ namespace Oqtane.Repository public interface ISiteRepository { IEnumerable GetSites(); + Task> GetSitesAsync(); Site AddSite(Site site); + Task AddSiteAsync(Site site); Site UpdateSite(Site site); + Task UpdateSiteAsync(Site site); Site GetSite(int siteId); + Task GetSiteAsync(int siteId); Site GetSite(int siteId, bool tracking); + Task GetSiteAsync(int siteId, bool tracking); void DeleteSite(int siteId); + Task DeleteSiteAsync(int siteId); void InitializeSite(Alias alias); void CreatePages(Site site, List pageTemplates, Alias alias); } diff --git a/Oqtane.Server/Repository/SiteRepository.cs b/Oqtane.Server/Repository/SiteRepository.cs index 234a16a38..8fc39667e 100644 --- a/Oqtane.Server/Repository/SiteRepository.cs +++ b/Oqtane.Server/Repository/SiteRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -52,17 +53,29 @@ public SiteRepository(TenantDBContext context, IRoleRepository roleRepository, I public IEnumerable GetSites() { - return _db.Site; + return _db.Site.OrderBy(item => item.Name); + } + public async Task> GetSitesAsync() + { + return await _db.Site.OrderBy(item => item.Name).ToListAsync(); } public Site AddSite(Site site) { - site.SiteGuid = System.Guid.NewGuid().ToString(); + site.SiteGuid = Guid.NewGuid().ToString(); _db.Site.Add(site); _db.SaveChanges(); CreateSite(site); return site; } + public async Task AddSiteAsync(Site site) + { + site.SiteGuid = Guid.NewGuid().ToString(); + _db.Site.Add(site); + await _db.SaveChangesAsync(); + CreateSite(site); + return site; + } public Site UpdateSite(Site site) { @@ -70,11 +83,21 @@ public Site UpdateSite(Site site) _db.SaveChanges(); return site; } + public async Task UpdateSiteAsync(Site site) + { + _db.Entry(site).State = EntityState.Modified; + await _db.SaveChangesAsync(); + return site; + } public Site GetSite(int siteId) { return GetSite(siteId, true); } + public async Task GetSiteAsync(int siteId) + { + return await GetSiteAsync(siteId, true); + } public Site GetSite(int siteId, bool tracking) { @@ -87,6 +110,17 @@ public Site GetSite(int siteId, bool tracking) return _db.Site.AsNoTracking().FirstOrDefault(item => item.SiteId == siteId); } } + public async Task GetSiteAsync(int siteId, bool tracking) + { + if (tracking) + { + return await _db.Site.FindAsync(siteId); + } + else + { + return await _db.Site.AsNoTracking().FirstOrDefaultAsync(item => item.SiteId == siteId); + } + } public void DeleteSite(int siteId) { @@ -94,6 +128,12 @@ public void DeleteSite(int siteId) _db.Site.Remove(site); _db.SaveChanges(); } + public async Task DeleteSiteAsync(int siteId) + { + var site = await _db.Site.FindAsync(siteId); + _db.Site.Remove(site); + _db.SaveChanges(); + } public void InitializeSite(Alias alias) { diff --git a/Oqtane.Server/Services/SiteService.cs b/Oqtane.Server/Services/SiteService.cs index 237cac2fd..f94d3f3f2 100644 --- a/Oqtane.Server/Services/SiteService.cs +++ b/Oqtane.Server/Services/SiteService.cs @@ -55,37 +55,35 @@ public async Task> GetSitesAsync() List sites = new List(); if (_accessor.HttpContext.User.IsInRole(RoleNames.Host)) { - sites = _sites.GetSites().OrderBy(item => item.Name).ToList(); + sites = (await _sites.GetSitesAsync()).ToList(); } - return await Task.Run(() => sites); + return sites; } public async Task GetSiteAsync(int siteId) { - Site site = null; if (!_accessor.HttpContext.User.Identity.IsAuthenticated) { - site = _cache.GetOrCreate($"site:{_accessor.HttpContext.GetAlias().SiteKey}", entry => + return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}", async entry => { entry.SlidingExpiration = TimeSpan.FromMinutes(30); - return GetSite(siteId); + return await GetSite(siteId); }, true); } else // authenticated - cached per user { - site = _cache.GetOrCreate($"site:{_accessor.HttpContext.GetAlias().SiteKey}:{_accessor.HttpContext.User.UserId}", entry => + return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}:{_accessor.HttpContext.User.UserId}", async entry => { entry.SlidingExpiration = TimeSpan.FromMinutes(30); - return GetSite(siteId); + return await GetSite(siteId); }, true); } - return await Task.Run(() => site); } - private Site GetSite(int siteid) + private async Task GetSite(int siteid) { var alias = _tenantManager.GetAlias(); - var site = _sites.GetSite(siteid); + var site = await _sites.GetSiteAsync(siteid); if (site != null && site.SiteId == alias.SiteId) { // site settings @@ -184,7 +182,7 @@ public async Task AddSiteAsync(Site site) { if (_accessor.HttpContext.User.IsInRole(RoleNames.Host)) { - site = _sites.AddSite(site); + site = await _sites.AddSiteAsync(site); _syncManager.AddSyncEvent(_tenantManager.GetAlias(), EntityNames.Site, site.SiteId, SyncEventActions.Create); _logger.Log(site.SiteId, LogLevel.Information, this, LogFunction.Create, "Site Added {Site}", site); } @@ -192,7 +190,7 @@ public async Task AddSiteAsync(Site site) { site = null; } - return await Task.Run(() => site); + return site; } public async Task UpdateSiteAsync(Site site) @@ -200,10 +198,10 @@ public async Task UpdateSiteAsync(Site site) if (_accessor.HttpContext.User.IsInRole(RoleNames.Admin)) { var alias = _tenantManager.GetAlias(); - var current = _sites.GetSite(site.SiteId, false); + var current = await _sites.GetSiteAsync(site.SiteId, false); if (site.SiteId == alias.SiteId && site.TenantId == alias.TenantId && current != null) { - site = _sites.UpdateSite(site); + site = await _sites.UpdateSiteAsync(site); _syncManager.AddSyncEvent(alias, EntityNames.Site, site.SiteId, SyncEventActions.Update); string action = SyncEventActions.Refresh; if (current.RenderMode != site.RenderMode || current.Runtime != site.Runtime) @@ -223,7 +221,7 @@ public async Task UpdateSiteAsync(Site site) { site = null; } - return await Task.Run(() => site); + return site; } public async Task DeleteSiteAsync(int siteId) @@ -231,10 +229,10 @@ public async Task DeleteSiteAsync(int siteId) if (_accessor.HttpContext.User.IsInRole(RoleNames.Host)) { var alias = _tenantManager.GetAlias(); - var site = _sites.GetSite(siteId); + var site = await _sites.GetSiteAsync(siteId); if (site != null && site.SiteId == alias.SiteId) { - _sites.DeleteSite(siteId); + await _sites.DeleteSiteAsync(siteId); _syncManager.AddSyncEvent(alias, EntityNames.Site, site.SiteId, SyncEventActions.Delete); _logger.Log(siteId, LogLevel.Information, this, LogFunction.Delete, "Site Deleted {SiteId}", siteId); } @@ -243,7 +241,6 @@ public async Task DeleteSiteAsync(int siteId) _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Site Delete Attempt {SiteId}", siteId); } } - await Task.CompletedTask; } private static List GetPagesHierarchy(List pages)