Core: fixed user cache

This commit is contained in:
pavelbannov 2022-08-31 21:06:26 +03:00
parent 49de93c89d
commit df96629834
4 changed files with 29 additions and 19 deletions

View File

@ -60,17 +60,12 @@ public class UserServiceCache
CacheGroupCacheItem = cacheGroupCacheItem;
CacheUserGroupRefItem = cacheUserGroupRefItem;
cacheUserInfoItem.Subscribe((u) => InvalidateCache(u), CacheNotifyAction.Any);
cacheUserInfoItem.Subscribe(InvalidateCache, CacheNotifyAction.Any);
cacheUserPhotoItem.Subscribe((p) => Cache.Remove(p.Key), CacheNotifyAction.Remove);
cacheGroupCacheItem.Subscribe((g) => InvalidateCache(), CacheNotifyAction.Any);
cacheGroupCacheItem.Subscribe((g) => InvalidateCache(g), CacheNotifyAction.Any);
cacheUserGroupRefItem.Subscribe((r) => UpdateUserGroupRefCache(r, true), CacheNotifyAction.Remove);
cacheUserGroupRefItem.Subscribe((r) => UpdateUserGroupRefCache(r, false), CacheNotifyAction.InsertOrUpdate);
}
public void InvalidateCache()
{
InvalidateCache(null);
cacheUserGroupRefItem.Subscribe((r) => UpdateUserGroupRefCache(r), CacheNotifyAction.Remove);
cacheUserGroupRefItem.Subscribe((r) => UpdateUserGroupRefCache(r), CacheNotifyAction.InsertOrUpdate);
}
private void InvalidateCache(UserInfoCacheItem userInfo)
@ -81,22 +76,26 @@ public class UserServiceCache
Cache.Remove(key);
}
}
private void InvalidateCache(GroupCacheItem groupCacheItem)
{
if (groupCacheItem != null)
{
var key = GetGroupCacheKey(groupCacheItem.Tenant, new Guid(groupCacheItem.Id));
Cache.Remove(key);
}
}
private void UpdateUserGroupRefCache(UserGroupRef r, bool remove)
private void UpdateUserGroupRefCache(UserGroupRef r)
{
var key = GetRefCacheKey(r.Tenant);
var refs = Cache.Get<UserGroupRefStore>(key);
if (!remove && refs != null)
if (refs != null)
{
lock (refs)
{
refs[r.CreateKey()] = r;
}
}
else
{
InvalidateCache();
}
}
public static string GetUserPhotoCacheKey(int tenant, Guid userId)
@ -294,7 +293,7 @@ public class CachedUserService : IUserService, ICachedService
public Group SaveGroup(int tenant, Group group)
{
group = Service.SaveGroup(tenant, group);
CacheGroupCacheItem.Publish(new GroupCacheItem { Id = group.Id.ToString() }, CacheNotifyAction.Any);
CacheGroupCacheItem.Publish(new GroupCacheItem { Id = group.Id.ToString(), Tenant = tenant }, CacheNotifyAction.Any);
return group;
}
@ -302,7 +301,7 @@ public class CachedUserService : IUserService, ICachedService
public void RemoveGroup(int tenant, Guid id)
{
Service.RemoveGroup(tenant, id);
CacheGroupCacheItem.Publish(new GroupCacheItem { Id = id.ToString() }, CacheNotifyAction.Any);
CacheGroupCacheItem.Publish(new GroupCacheItem { Id = id.ToString(), Tenant = tenant }, CacheNotifyAction.Any);
}
@ -348,7 +347,7 @@ public class CachedUserService : IUserService, ICachedService
{
Service.RemoveUserGroupRef(tenant, userId, groupId, refType);
var r = new UserGroupRef(userId, groupId, refType) { Tenant = tenant };
var r = new UserGroupRef(userId, groupId, refType) { Tenant = tenant, Removed = true };
CacheUserGroupRefItem.Publish(r, CacheNotifyAction.Remove);
}
@ -382,7 +381,6 @@ public class CachedUserService : IUserService, ICachedService
public void InvalidateCache()
{
UserServiceCache.InvalidateCache();
}
public UserInfo GetUser(int tenant, Guid id, Expression<Func<User, UserInfo>> exp)

View File

@ -65,6 +65,12 @@ public class UserGroupRef : IMapFrom<UserGroup>
return obj is UserGroupRef r && r.Tenant == Tenant && r.UserId == UserId && r.GroupId == GroupId && r.RefType == RefType;
}
public void Mapping(Profile profile)
{
profile.CreateMap<UserGroup, UserGroupRef>()
.ForMember(dest => dest.GroupId, opt => opt.MapFrom(src => src.UserGroupId));
}
public static implicit operator UserGroupRef(UserGroupRefCacheItem cache)
{
var result = new UserGroupRef

View File

@ -38,6 +38,11 @@ public class UserGroup : BaseEntity, IMapFrom<UserGroupRef>
{
return new object[] { Tenant, Userid, UserGroupId, RefType };
}
public void Mapping(Profile profile)
{
profile.CreateMap<UserGroupRef, UserGroup>()
.ForMember(dest => dest.UserGroupId, opt => opt.MapFrom(src => src.GroupId));
}
}
public static class DbUserGroupExtension

View File

@ -4,4 +4,5 @@ package ASC.Core.Caching;
message GroupCacheItem {
string id = 1;
int32 tenant = 2;
}