improve caching for sites with many registered users
This commit is contained in:
parent
7cc328ed3b
commit
0c8dc63085
|
@ -1,5 +1,6 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
using Oqtane.Shared;
|
||||||
|
|
||||||
namespace Oqtane.Extensions
|
namespace Oqtane.Extensions
|
||||||
{
|
{
|
||||||
|
@ -70,5 +71,17 @@ namespace Oqtane.Extensions
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsOnlyInRole(this ClaimsPrincipal claimsPrincipal, string role)
|
||||||
|
{
|
||||||
|
var identity = claimsPrincipal.Identities.FirstOrDefault(item => item.AuthenticationType == Constants.AuthenticationScheme);
|
||||||
|
if (identity != null)
|
||||||
|
{
|
||||||
|
// check if user has role claim specified and no other role claims
|
||||||
|
return identity.Claims.Any(item => item.Type == ClaimTypes.Role && item.Value == role) &&
|
||||||
|
!identity.Claims.Any(item => item.Type == ClaimTypes.Role && item.Value != role);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,13 +64,25 @@ namespace Oqtane.Services
|
||||||
{
|
{
|
||||||
if (!_accessor.HttpContext.User.Identity.IsAuthenticated)
|
if (!_accessor.HttpContext.User.Identity.IsAuthenticated)
|
||||||
{
|
{
|
||||||
|
// unauthenticated
|
||||||
return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}", async entry =>
|
return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}", async entry =>
|
||||||
{
|
{
|
||||||
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
||||||
return await GetSite(siteId);
|
return await GetSite(siteId);
|
||||||
}, true);
|
}, true);
|
||||||
}
|
}
|
||||||
else // authenticated - cached per user
|
else // authenticated
|
||||||
|
{
|
||||||
|
// is only in registered users role - cache by role
|
||||||
|
if (_accessor.HttpContext.User.IsOnlyInRole(RoleNames.Registered))
|
||||||
|
{
|
||||||
|
return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}:{RoleNames.Registered}", async entry =>
|
||||||
|
{
|
||||||
|
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
|
||||||
|
return await GetSite(siteId);
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
else // cache by user
|
||||||
{
|
{
|
||||||
return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}:{_accessor.HttpContext.User.UserId}", async entry =>
|
return await _cache.GetOrCreateAsync($"site:{_accessor.HttpContext.GetAlias().SiteKey}:{_accessor.HttpContext.User.UserId}", async entry =>
|
||||||
{
|
{
|
||||||
|
@ -79,6 +91,7 @@ namespace Oqtane.Services
|
||||||
}, true);
|
}, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<Site> GetSite(int siteid)
|
private async Task<Site> GetSite(int siteid)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user