Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache key to GetByUserName #17350

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading