From 511cea560bfc43ce1f1a0d3f2ef7a6d11295bf04 Mon Sep 17 00:00:00 2001 From: MaksimChegulov Date: Mon, 27 Sep 2021 19:59:09 +0300 Subject: [PATCH] add scoped caching --- .../Caching/CachedUserService.cs | 89 +++++++++---------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/common/ASC.Core.Common/Caching/CachedUserService.cs b/common/ASC.Core.Common/Caching/CachedUserService.cs index 6674eeecc2..da946cd77d 100644 --- a/common/ASC.Core.Common/Caching/CachedUserService.cs +++ b/common/ASC.Core.Common/Caching/CachedUserService.cs @@ -120,6 +120,11 @@ namespace ASC.Core.Caching public static string GetGroupCacheKey(int tenant) { return tenant.ToString() + GROUPS; + } + + public static string GetGroupCacheKey(int tenant, Guid groupId) + { + return tenant.ToString() + GROUPS + groupId; } public static string GetRefCacheKey(int tenant) @@ -179,7 +184,8 @@ namespace ASC.Core.Caching public class CachedUserService : IUserService, ICachedService { internal IUserService Service { get; set; } - internal ICache Cache { get; set; } + internal ICache Cache { get; set; } + internal ScopedCache scopedCache = new ScopedCache(); internal TrustInterval TrustInterval { get; set; } private int getchanges; @@ -248,18 +254,18 @@ namespace ASC.Core.Caching public UserInfo GetUser(int tenant, Guid id) { if (id.Equals(Guid.Empty)) return null; - - if (CoreBaseSettings.Personal) - { - return GetUserForPersonal(tenant, id); - } - - var users = GetUsers(tenant); - lock (users) - { - users.TryGetValue(id, out var u); - return u; - } + + var key = UserServiceCache.GetUserCacheKeyForPersonal(tenant, id); + var user = scopedCache.Get(key); + + if (user == null) + { + user = Service.GetUser(tenant, id); + + if (user != null) scopedCache.Insert(key, user); + } + + return user; } public UserInfo GetUser(int tenant, string email) @@ -306,15 +312,17 @@ namespace ASC.Core.Caching public UserInfo SaveUser(int tenant, UserInfo user) { - user = Service.SaveUser(tenant, user); - CacheUserInfoItem.Publish(new UserInfoCacheItem { ID = user.ID.ToByteString(), Tenant = tenant }, CacheNotifyAction.Any); + user = Service.SaveUser(tenant, user); + scopedCache.Remove(UserServiceCache.GetUserCacheKeyForPersonal(tenant, user.ID)); + //CacheUserInfoItem.Publish(new UserInfoCacheItem { ID = user.ID.ToByteString(), Tenant = tenant }, CacheNotifyAction.Any); return user; } public void RemoveUser(int tenant, Guid id) { - Service.RemoveUser(tenant, id); - CacheUserInfoItem.Publish(new UserInfoCacheItem { Tenant = tenant, ID = id.ToByteString() }, CacheNotifyAction.Any); + Service.RemoveUser(tenant, id); + scopedCache.Remove(UserServiceCache.GetUserCacheKeyForPersonal(tenant, id)); + //CacheUserInfoItem.Publish(new UserInfoCacheItem { Tenant = tenant, ID = id.ToByteString() }, CacheNotifyAction.Any); } public byte[] GetUserPhoto(int tenant, Guid id) @@ -348,21 +356,24 @@ namespace ASC.Core.Caching public IDictionary GetGroups(int tenant, DateTime from) { - var groups = GetGroups(tenant); - lock (groups) - { - return (from == default ? groups.Values : groups.Values.Where(g => g.LastModified >= from)).ToDictionary(g => g.Id); - } + var groups = GetGroups(tenant); + + return (from == default ? groups.Values : groups.Values.Where(g => g.LastModified >= from)).ToDictionary(g => g.Id); } public Group GetGroup(int tenant, Guid id) - { - var groups = GetGroups(tenant); - lock (groups) - { - groups.TryGetValue(id, out var g); - return g; - } + { + var key = UserServiceCache.GetGroupCacheKey(tenant, id); + var group = scopedCache.Get(key); + + if (group == null) + { + group = Service.GetGroup(tenant, id); + + if (group != null) scopedCache.Insert(key, group); + }; + + return group; } public Group SaveGroup(int tenant, Group group) @@ -418,30 +429,14 @@ namespace ASC.Core.Caching private IDictionary GetUsers(int tenant) { - GetChangesFromDb(); - - var key = UserServiceCache.GetUserCacheKey(tenant); - var users = Cache.Get>(key); - if (users == null) - { - users = Service.GetUsers(tenant, default); - - Cache.Insert(key, users, CacheExpiration); - } + var users = Service.GetUsers(tenant, default); return users; } private IDictionary GetGroups(int tenant) { - GetChangesFromDb(); + var groups = Service.GetGroups(tenant, default); - var key = UserServiceCache.GetGroupCacheKey(tenant); - var groups = Cache.Get>(key); - if (groups == null) - { - groups = Service.GetGroups(tenant, default); - Cache.Insert(key, groups, CacheExpiration); - } return groups; }