private IDictionary <Guid, Group> GetGroups(int tenant) { GetChangesFromDb(); var key = UserServiceCache.GetGroupCacheKey(tenant); var groups = Cache.Get <IDictionary <Guid, Group> >(key); if (groups == null) { groups = Service.GetGroups(tenant, default); Cache.Insert(key, groups, CacheExpiration); } return(groups); }
private void GetChangesFromDb() { if (!TrustInterval.Expired) { return; } if (Interlocked.CompareExchange(ref getchanges, 1, 0) == 0) { try { if (!TrustInterval.Expired) { return; } var starttime = TrustInterval.StartTime; if (starttime != default) { var correction = TimeSpan.FromTicks(DbExpiration.Ticks * 3); starttime = TrustInterval.StartTime.Subtract(correction); } TrustInterval.Start(DbExpiration); //get and merge changes in cached tenants foreach (var tenantGroup in Service.GetUsers(Tenant.DEFAULT_TENANT, starttime).Values.GroupBy(u => u.Tenant)) { var users = Cache.Get <IDictionary <Guid, UserInfo> >(UserServiceCache.GetUserCacheKey(tenantGroup.Key)); if (users != null) { lock (users) { foreach (var u in tenantGroup) { users[u.ID] = u; } } } } foreach (var tenantGroup in Service.GetGroups(Tenant.DEFAULT_TENANT, starttime).Values.GroupBy(g => g.Tenant)) { var groups = Cache.Get <IDictionary <Guid, Group> >(UserServiceCache.GetGroupCacheKey(tenantGroup.Key)); if (groups != null) { lock (groups) { foreach (var g in tenantGroup) { groups[g.Id] = g; } } } } foreach (var tenantGroup in Service.GetUserGroupRefs(Tenant.DEFAULT_TENANT, starttime).Values.GroupBy(r => r.Tenant)) { var refs = Cache.Get <UserGroupRefStore>(UserServiceCache.GetRefCacheKey(tenantGroup.Key)); if (refs != null) { lock (refs) { foreach (var r in tenantGroup) { refs[r.CreateKey()] = r; } } } } } finally { Volatile.Write(ref getchanges, 0); } } }