From cd0fcd4f1ad48c7fc3f4bc7ff4d192fd66ef77ba Mon Sep 17 00:00:00 2001 From: lulz1 Date: Thu, 4 Jul 2024 16:09:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=9B=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=AF=BC=E8=87=B4=E8=A1=8C=E7=A8=8B=E8=A7=84=E5=88=92?= =?UTF-8?q?=E5=8D=95=E6=9D=83=E9=99=90=E6=B6=88=E5=A4=B1=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PermissionConfigRepositoryImpl.java | 15 ++++++++++----- .../cache/CachePermissionConfigRepository.java | 5 +++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java index 36e36328..34f5420a 100644 --- a/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java @@ -5,7 +5,6 @@ import com.chint.domain.repository.PermissionConfigRepository; import com.chint.infrastructure.repository.cache.CachePermissionConfigRepository; import com.chint.infrastructure.repository.jdbc.JdbcPermissionConfigRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Repository; import java.util.*; @@ -20,12 +19,18 @@ public class PermissionConfigRepositoryImpl implements PermissionConfigRepositor @Autowired private CachePermissionConfigRepository cachePermissionConfigRepository; - @Cacheable(value = "PermissionConfig", key = "#permissionName") + @Override public Optional findByPermissionName(String permissionName) { - return jdbcPermissionConfigRepository.findByPermissionName(permissionName) - .stream() - .findFirst(); + PermissionConfig permissionConfig = cachePermissionConfigRepository.cacheByName(permissionName, null); + if (permissionConfig == null) { + Optional first = jdbcPermissionConfigRepository.findByPermissionName(permissionName) + .stream() + .findFirst(); + first.ifPresent(it -> cachePermissionConfigRepository.cacheByName(it)); + return first; + } + return Optional.of(permissionConfig); } @Override diff --git a/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java b/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java index 03d15bff..5f78c12c 100644 --- a/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java @@ -40,6 +40,11 @@ public class CachePermissionConfigRepository { return permissionConfig; } + @Cacheable(value = "PermissionConfig", key = "#permissionConfigName") + public PermissionConfig cacheByName(String permissionConfigName, PermissionConfig permissionConfig) { + return permissionConfig; + } + @Cacheable(value = "PermissionConfig", key = "#permissionName") public void cacheEvictByName(String permissionName) { }