From 5093ec7fb57a564ca304be11881dc884ea78abf3 Mon Sep 17 00:00:00 2001 From: Zeegaan Date: Thu, 24 Oct 2024 13:02:02 +0200 Subject: [PATCH 1/3] Add cache key to GetByUserName --- .../Persistence/Repositories/Implement/MemberRepository.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs index c89344716ff1..f622e273c033 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs @@ -228,7 +228,7 @@ public override IEnumerable GetPage(IQuery? query, } public IMember? GetByUsername(string? username) => - _memberByUsernameCachePolicy.Get(username, PerformGetByUsername, PerformGetAllByUsername); + _memberByUsernameCachePolicy.Get("uRepo_userNameKey+" + username, PerformGetByUsername, PerformGetAllByUsername); public int[] GetMemberIds(string[] usernames) { @@ -310,6 +310,8 @@ protected override string ApplySystemOrdering(ref Sql sql, Ordering return SqlSyntax.GetFieldName(x => x.LastPasswordChangeDate); } + Constants. + return base.ApplySystemOrdering(ref sql, ordering); } From 9fe7fc9408305c069ab9a0e90c3e1d9519684163 Mon Sep 17 00:00:00 2001 From: Zeegaan Date: Thu, 24 Oct 2024 13:19:41 +0200 Subject: [PATCH 2/3] Remove constants --- .../Persistence/Repositories/Implement/MemberRepository.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs index f622e273c033..abd21e7f4bd3 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs @@ -310,8 +310,6 @@ protected override string ApplySystemOrdering(ref Sql sql, Ordering return SqlSyntax.GetFieldName(x => x.LastPasswordChangeDate); } - Constants. - return base.ApplySystemOrdering(ref sql, ordering); } From 246f5c8241415f82f812aaae0fa82c85be9cba00 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:46:51 +0100 Subject: [PATCH 3/3] create new cache policy for member repository --- .../MemberRepositoryUsernameCachePolicy.cs | 33 +++++++++++++++++++ .../Implement/MemberRepository.cs | 6 ++-- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/Umbraco.Infrastructure/Cache/MemberRepositoryUsernameCachePolicy.cs diff --git a/src/Umbraco.Infrastructure/Cache/MemberRepositoryUsernameCachePolicy.cs b/src/Umbraco.Infrastructure/Cache/MemberRepositoryUsernameCachePolicy.cs new file mode 100644 index 000000000000..1dc5f42a0117 --- /dev/null +++ b/src/Umbraco.Infrastructure/Cache/MemberRepositoryUsernameCachePolicy.cs @@ -0,0 +1,33 @@ +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Infrastructure.Scoping; +using Umbraco.Extensions; + +namespace Umbraco.Cms.Core.Cache; + +public class MemberRepositoryUsernameCachePolicy : DefaultRepositoryCachePolicy +{ + public MemberRepositoryUsernameCachePolicy(IAppPolicyCache cache, IScopeAccessor scopeAccessor, RepositoryCachePolicyOptions options) : base(cache, scopeAccessor, options) + { + } + + public IMember? GetByUserName(string key, string? username, Func performGetByUsername, Func?> performGetAll) + { + var cacheKey = GetEntityCacheKey(key + username); + IMember? fromCache = Cache.GetCacheItem(cacheKey); + + // if found in cache then return else fetch and cache + if (fromCache != null) + { + return fromCache; + } + + IMember? entity = performGetByUsername(username); + + if (entity != null && entity.HasIdentity) + { + InsertEntity(cacheKey, entity); + } + + return entity; + } +} diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs index abd21e7f4bd3..053cb15b2d08 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberRepository.cs @@ -30,7 +30,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement; public class MemberRepository : ContentRepositoryBase, IMemberRepository { private readonly IJsonSerializer _jsonSerializer; - private readonly IRepositoryCachePolicy _memberByUsernameCachePolicy; + private readonly MemberRepositoryUsernameCachePolicy _memberByUsernameCachePolicy; private readonly IMemberGroupRepository _memberGroupRepository; private readonly IMemberTypeRepository _memberTypeRepository; private readonly MemberPasswordConfigurationSettings _passwordConfiguration; @@ -67,7 +67,7 @@ public MemberRepository( _memberGroupRepository = memberGroupRepository; _passwordConfiguration = passwordConfiguration.Value; _memberByUsernameCachePolicy = - new DefaultRepositoryCachePolicy(GlobalIsolatedCache, ScopeAccessor, DefaultOptions); + new MemberRepositoryUsernameCachePolicy(GlobalIsolatedCache, ScopeAccessor, DefaultOptions); } /// @@ -228,7 +228,7 @@ public override IEnumerable GetPage(IQuery? query, } public IMember? GetByUsername(string? username) => - _memberByUsernameCachePolicy.Get("uRepo_userNameKey+" + username, PerformGetByUsername, PerformGetAllByUsername); + _memberByUsernameCachePolicy.GetByUserName("uRepo_userNameKey+", username, PerformGetByUsername, PerformGetAllByUsername); public int[] GetMemberIds(string[] usernames) {