From ba4120050f5d280095fc1bc6ec0c47779ca29415 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:27:22 +0100 Subject: [PATCH] Add cache key to GetByUserName (#17350) * Add cache key to GetByUserName * Remove constants * create new cache policy for member repository --------- Co-authored-by: Elitsa --- .../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 c89344716ff1..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(username, PerformGetByUsername, PerformGetAllByUsername); + _memberByUsernameCachePolicy.GetByUserName("uRepo_userNameKey+", username, PerformGetByUsername, PerformGetAllByUsername); public int[] GetMemberIds(string[] usernames) {