Skip to content

Commit

Permalink
Add cache key to GetByUserName (#17350)
Browse files Browse the repository at this point in the history
* Add cache key to GetByUserName

* Remove constants

* create new cache policy for member repository

---------

Co-authored-by: Elitsa <[email protected]>
  • Loading branch information
Zeegaan and elit0451 authored Nov 25, 2024
1 parent cbd4dc6 commit ba41200
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<IMember, string>
{
public MemberRepositoryUsernameCachePolicy(IAppPolicyCache cache, IScopeAccessor scopeAccessor, RepositoryCachePolicyOptions options) : base(cache, scopeAccessor, options)
{
}

public IMember? GetByUserName(string key, string? username, Func<string?, IMember?> performGetByUsername, Func<string[]?, IEnumerable<IMember>?> performGetAll)
{
var cacheKey = GetEntityCacheKey(key + username);
IMember? fromCache = Cache.GetCacheItem<IMember>(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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
public class MemberRepository : ContentRepositoryBase<int, IMember, MemberRepository>, IMemberRepository
{
private readonly IJsonSerializer _jsonSerializer;
private readonly IRepositoryCachePolicy<IMember, string> _memberByUsernameCachePolicy;
private readonly MemberRepositoryUsernameCachePolicy _memberByUsernameCachePolicy;
private readonly IMemberGroupRepository _memberGroupRepository;
private readonly IMemberTypeRepository _memberTypeRepository;
private readonly MemberPasswordConfigurationSettings _passwordConfiguration;
Expand Down Expand Up @@ -67,7 +67,7 @@ public MemberRepository(
_memberGroupRepository = memberGroupRepository;
_passwordConfiguration = passwordConfiguration.Value;
_memberByUsernameCachePolicy =
new DefaultRepositoryCachePolicy<IMember, string>(GlobalIsolatedCache, ScopeAccessor, DefaultOptions);
new MemberRepositoryUsernameCachePolicy(GlobalIsolatedCache, ScopeAccessor, DefaultOptions);
}

/// <summary>
Expand Down Expand Up @@ -228,7 +228,7 @@ public override IEnumerable<IMember> GetPage(IQuery<IMember>? query,
}

public IMember? GetByUsername(string? username) =>
_memberByUsernameCachePolicy.Get(username, PerformGetByUsername, PerformGetAllByUsername);
_memberByUsernameCachePolicy.GetByUserName("uRepo_userNameKey+", username, PerformGetByUsername, PerformGetAllByUsername);

public int[] GetMemberIds(string[] usernames)
{
Expand Down

0 comments on commit ba41200

Please sign in to comment.