Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Task.Run() logic in SiteService with async SiteRepository methods #3911

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions Oqtane.Server/Extensions/CacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Oqtane.Extensions
{
Expand All @@ -19,23 +21,45 @@ public static TItem GetOrCreate<TItem>(this IMemoryCache cache, string key, Func

if (track)
{
// track the cache key
List<string> cachekeys;
if (!cache.TryGetValue(_cachekeys, out cachekeys))
{
cachekeys = new List<string>();
}
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<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache, string key, Func<ICacheEntry, Task<TItem>> 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<string> cachekeys;
if (!cache.TryGetValue(_cachekeys, out cachekeys))
{
cachekeys = new List<string>();
}
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<string> cachekeys;
Expand Down
7 changes: 7 additions & 0 deletions Oqtane.Server/Repository/Interfaces/ISiteRepository.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Oqtane.Models;

namespace Oqtane.Repository
{
public interface ISiteRepository
{
IEnumerable<Site> GetSites();
Task<IEnumerable<Site>> GetSitesAsync();
Site AddSite(Site site);
Task<Site> AddSiteAsync(Site site);
Site UpdateSite(Site site);
Task<Site> UpdateSiteAsync(Site site);
Site GetSite(int siteId);
Task<Site> GetSiteAsync(int siteId);
Site GetSite(int siteId, bool tracking);
Task<Site> GetSiteAsync(int siteId, bool tracking);
void DeleteSite(int siteId);
Task DeleteSiteAsync(int siteId);
void InitializeSite(Alias alias);
void CreatePages(Site site, List<PageTemplate> pageTemplates, Alias alias);
}
Expand Down
44 changes: 42 additions & 2 deletions Oqtane.Server/Repository/SiteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,29 +53,51 @@ public SiteRepository(TenantDBContext context, IRoleRepository roleRepository, I

public IEnumerable<Site> GetSites()
{
return _db.Site;
return _db.Site.OrderBy(item => item.Name);
}
public async Task<IEnumerable<Site>> 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<Site> AddSiteAsync(Site site)
{
site.SiteGuid = Guid.NewGuid().ToString();
_db.Site.Add(site);
await _db.SaveChangesAsync();
CreateSite(site);
return site;
}

public Site UpdateSite(Site site)
{
_db.Entry(site).State = EntityState.Modified;
_db.SaveChanges();
return site;
}
public async Task<Site> 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<Site> GetSiteAsync(int siteId)
{
return await GetSiteAsync(siteId, true);
}

public Site GetSite(int siteId, bool tracking)
{
Expand All @@ -87,13 +110,30 @@ public Site GetSite(int siteId, bool tracking)
return _db.Site.AsNoTracking().FirstOrDefault(item => item.SiteId == siteId);
}
}
public async Task<Site> 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)
{
var site = _db.Site.Find(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)
{
Expand Down
33 changes: 15 additions & 18 deletions Oqtane.Server/Services/SiteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,35 @@ public async Task<List<Site>> GetSitesAsync()
List<Site> sites = new List<Site>();
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<Site> 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<Site> 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
Expand Down Expand Up @@ -184,26 +182,26 @@ public async Task<Site> 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);
}
else
{
site = null;
}
return await Task.Run(() => site);
return site;
}

public async Task<Site> 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)
Expand All @@ -223,18 +221,18 @@ public async Task<Site> UpdateSiteAsync(Site site)
{
site = null;
}
return await Task.Run(() => site);
return site;
}

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);
}
Expand All @@ -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<Page> GetPagesHierarchy(List<Page> pages)
Expand Down