V14: Cache user by id (#16139)

* Cache user by id

* Add todo

* Fix tests

* Clear cache after update

* Refer to base class cache instead of implementing own

* Dont hardcode cache key

* Update to be Payload cache refresher instead
This commit is contained in:
Nikolaj Geisle
2024-04-25 15:56:01 +02:00
committed by GitHub
parent f824280162
commit 7bd499565d
6 changed files with 96 additions and 37 deletions

View File

@@ -22,22 +22,18 @@ public static class DistributedCacheExtensions
#region UserCacheRefresher
public static void RemoveUserCache(this DistributedCache dc, int userId)
=> dc.Remove(UserCacheRefresher.UniqueId, userId);
public static void RemoveUserCache(this DistributedCache dc, IEnumerable<IUser> users)
=> dc.Remove(UserCacheRefresher.UniqueId, users.Select(x => x.Id).Distinct().ToArray());
public static void RefreshUserCache(this DistributedCache dc, int userId)
=> dc.Refresh(UserCacheRefresher.UniqueId, userId);
public static void RefreshUserCache(this DistributedCache dc, IEnumerable<IUser> users)
{
foreach (IUser user in users)
IEnumerable<UserCacheRefresher.JsonPayload> payloads = users.Select(x => new UserCacheRefresher.JsonPayload()
{
dc.Refresh(UserCacheRefresher.UniqueId, user.Key);
dc.Refresh(UserCacheRefresher.UniqueId, user.Id);
}
Id = x.Id,
Key = x.Key,
});
dc.RefreshByPayload(UserCacheRefresher.UniqueId, payloads);
}
public static void RefreshAllUserCache(this DistributedCache dc)

View File

@@ -2,27 +2,29 @@ using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Serialization;
namespace Umbraco.Cms.Core.Cache;
public sealed class UserCacheRefresher : CacheRefresherBase<UserCacheRefresherNotification>
public sealed class UserCacheRefresher : PayloadCacheRefresherBase<UserCacheRefresherNotification, UserCacheRefresher.JsonPayload>
{
#region Define
public static readonly Guid UniqueId = Guid.Parse("E057AF6D-2EE6-41F4-8045-3694010F0AA6");
public UserCacheRefresher(AppCaches appCaches, IEventAggregator eventAggregator, ICacheRefresherNotificationFactory factory)
: base(appCaches, eventAggregator, factory)
public UserCacheRefresher(AppCaches appCaches, IJsonSerializer serializer, IEventAggregator eventAggregator, ICacheRefresherNotificationFactory factory)
: base(appCaches, serializer, eventAggregator, factory)
{
}
public static readonly Guid UniqueId = Guid.Parse("E057AF6D-2EE6-41F4-8045-3694010F0AA6");
public override Guid RefresherUniqueId => UniqueId;
public override string Name => "User Cache Refresher";
#endregion
public record JsonPayload
{
public int Id { get; init; }
#region Refresher
public Guid Key { get; init; }
}
public override void RefreshAll()
{
@@ -30,20 +32,28 @@ public sealed class UserCacheRefresher : CacheRefresherBase<UserCacheRefresherNo
base.RefreshAll();
}
public override void Refresh(Guid id)
public override void Refresh(JsonPayload[] payloads)
{
Attempt<IAppPolicyCache?> userCache = AppCaches.IsolatedCaches.Get<IUser>();
if (userCache.Success)
{
userCache.Result?.Clear(RepositoryCacheKeys.GetKey<IUser, Guid>(id));
userCache.Result?.ClearByKey(CacheKeys.UserContentStartNodePathsPrefix + id);
userCache.Result?.ClearByKey(CacheKeys.UserMediaStartNodePathsPrefix + id);
userCache.Result?.ClearByKey(CacheKeys.UserAllContentStartNodesPrefix + id);
userCache.Result?.ClearByKey(CacheKeys.UserAllMediaStartNodesPrefix + id);
}
base.Refresh(id);
ClearCache(payloads);
base.Refresh(payloads);
}
#endregion
private void ClearCache(params JsonPayload[] payloads)
{
foreach (JsonPayload p in payloads)
{
Attempt<IAppPolicyCache?> userCache = AppCaches.IsolatedCaches.Get<IUser>();
if (!userCache.Success)
{
continue;
}
userCache.Result?.Clear(RepositoryCacheKeys.GetKey<IUser, Guid>(p.Key));
userCache.Result?.Clear(RepositoryCacheKeys.GetKey<IUser, int>(p.Id));
userCache.Result?.ClearByKey(CacheKeys.UserContentStartNodePathsPrefix + p.Key);
userCache.Result?.ClearByKey(CacheKeys.UserMediaStartNodePathsPrefix + p.Key);
userCache.Result?.ClearByKey(CacheKeys.UserAllContentStartNodesPrefix + p.Key);
userCache.Result?.ClearByKey(CacheKeys.UserAllMediaStartNodesPrefix + p.Key);
}
}
}

View File

@@ -20,6 +20,15 @@ public interface IUserRepository : IReadWriteQueryRepository<Guid, IUser>
/// <returns></returns>
bool ExistsByUserName(string username);
/// <summary>
/// Returns a user by id
/// </summary>
/// <param name="id"></param>
/// <returns>
/// A cached <see cref="IUser" /> instance
/// </returns>
IUser? Get(int id);
/// <summary>
/// Checks if a user with the login exists
/// </summary>