From 472e1dc89c2cf257692ccec66c297972e129a81e Mon Sep 17 00:00:00 2001 From: zhouhao Date: Sat, 27 Sep 2025 15:03:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=9D=83?= =?UTF-8?q?=E9=99=90=E5=88=9D=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserAuthenticationEventPublisher.java | 2 +- .../auth/service/DefaultMenuService.java | 35 ++++++++++++++----- .../service/NotifySubscriberService.java | 26 ++++++++------ .../src/main/resources/application.yml | 2 ++ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/dimension/UserAuthenticationEventPublisher.java b/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/dimension/UserAuthenticationEventPublisher.java index 2c12cebf..5a689200 100644 --- a/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/dimension/UserAuthenticationEventPublisher.java +++ b/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/dimension/UserAuthenticationEventPublisher.java @@ -80,7 +80,7 @@ public class UserAuthenticationEventPublisher { private Mono publish0(Collection userIdList) { return Flux .fromIterable(userIdList) - .flatMapDelayError( + .flatMap( userId -> eventBus .publish(Topics.Authentications.userAuthenticationChanged(userId), ReactiveAuthenticationHolder diff --git a/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/service/DefaultMenuService.java b/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/service/DefaultMenuService.java index bc663660..c1ed5f19 100755 --- a/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/service/DefaultMenuService.java +++ b/jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/service/DefaultMenuService.java @@ -15,6 +15,7 @@ */ package org.jetlinks.community.auth.service; +import com.google.common.cache.CacheBuilder; import lombok.Generated; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; @@ -50,6 +51,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.math.MathFlux; +import java.time.Duration; import java.util.*; import java.util.function.Function; import java.util.function.Predicate; @@ -161,6 +163,13 @@ public class DefaultMenuService .as(this::convertToView); } + final Map, Flux> grantedCaching = + CacheBuilder + .newBuilder() + .expireAfterAccess(Duration.ofSeconds(10)) + ., Flux>build() + .asMap(); + public Flux getGrantedMenus(List dimensions, Mono> menuEntityMap) { if (CollectionUtils.isEmpty(dimensions)) { @@ -170,16 +179,25 @@ public class DefaultMenuService .stream() .filter(this::isMenuDimension) .map(dimension -> MenuBindEntity.generateTargetKey(dimension.getType().getId(), dimension.getId())) + .sorted() .collect(Collectors.toList()); - return convertToView(CollectionUtils.isEmpty(keyList) - ? Flux.empty() - : bindRepository - .createQuery() - .where() - .in(MenuBindEntity::getTargetKey, keyList) - .fetch(), - menuEntityMap); + return grantedCaching + .computeIfAbsent(keyList, + _keyList -> Flux + .defer(() -> this + .convertToView(CollectionUtils.isEmpty(_keyList) + ? Flux.empty() + : bindRepository + .createQuery() + .where() + .in(MenuBindEntity::getTargetKey, _keyList) + .fetch(), + menuEntityMap)) + // 缓存1秒钟,避免编辑角色或者组织时,导致大量用户权限失效并进行初始化时执行大量重复的查询. + .cache(Duration.ofSeconds(1))); + + } private boolean isMenuDimension(Dimension dimension) { @@ -204,7 +222,6 @@ public class DefaultMenuService } - private Flux convertToView(Flux entityFlux, Mono> menuEntityMap) { return Mono .zip( diff --git a/jetlinks-manager/notify-manager/src/main/java/org/jetlinks/community/notify/manager/service/NotifySubscriberService.java b/jetlinks-manager/notify-manager/src/main/java/org/jetlinks/community/notify/manager/service/NotifySubscriberService.java index 24e1a58a..af0bd5be 100644 --- a/jetlinks-manager/notify-manager/src/main/java/org/jetlinks/community/notify/manager/service/NotifySubscriberService.java +++ b/jetlinks-manager/notify-manager/src/main/java/org/jetlinks/community/notify/manager/service/NotifySubscriberService.java @@ -17,6 +17,7 @@ package org.jetlinks.community.notify.manager.service; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository; import org.hswebframework.web.authorization.Authentication; @@ -26,6 +27,8 @@ import org.hswebframework.web.crud.service.GenericReactiveCrudService; import org.hswebframework.web.exception.BusinessException; import org.hswebframework.web.i18n.LocaleUtils; import org.jetlinks.community.gateway.annotation.Subscribe; +import org.jetlinks.community.lock.ReactiveLock; +import org.jetlinks.community.lock.ReactiveLockHolder; import org.jetlinks.community.notify.manager.configuration.NotifySubscriberProperties; import org.jetlinks.community.notify.manager.entity.Notification; import org.jetlinks.community.notify.manager.entity.NotifySubscriberChannelEntity; @@ -56,6 +59,7 @@ import reactor.core.publisher.Mono; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; @Service @@ -463,13 +467,9 @@ public class NotifySubscriberService extends GenericReactiveCrudService new Node()) - .init(entity); + subs.computeIfAbsent(entity.getId(), ignore -> new Node()).init(entity); } @Override @@ -521,8 +520,15 @@ public class NotifySubscriberService extends GenericReactiveCrudService { initNotifyChannels(tp2.getT1()); return tp2.getT2().createSubscriber(entity.getId(), tp2.getT1(), entity.getTopicConfig()); diff --git a/jetlinks-standalone/src/main/resources/application.yml b/jetlinks-standalone/src/main/resources/application.yml index a05a0c14..1344ff7e 100644 --- a/jetlinks-standalone/src/main/resources/application.yml +++ b/jetlinks-standalone/src/main/resources/application.yml @@ -120,6 +120,8 @@ hsweb: # allopatric-login-modes: # app: offlineOther permission: + initialize: + enabled-dimensions: api-client filter: enabled: true # 设置为true开启权限过滤,赋权时,不能赋予比自己多的权限. exclude-username: admin # admin用户不受上述限制