refactor(service): replace manual caching with @Cacheable and JPQL with Criteria API (#316)
- Replace AppSettingService double-checked locking (volatile + ReentrantLock) with Spring @Cacheable/@CacheEvict on appSettings cache - Add appSettings cache name to CacheConfig - Replace NotificationService dynamic JPQL string concatenation with type-safe JPA Criteria API (CriteriaBuilder predicates)
This commit is contained in:
@@ -15,7 +15,7 @@ public class CacheConfig {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
CaffeineCacheManager cacheManager = new CaffeineCacheManager("publicSettings");
|
||||
CaffeineCacheManager cacheManager = new CaffeineCacheManager("publicSettings", "appSettings");
|
||||
cacheManager.setCaffeine(Caffeine.newBuilder()
|
||||
.expireAfterWrite(Duration.ofHours(24))
|
||||
.maximumSize(100));
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package org.booklore.service;
|
||||
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.enums.PermissionType;
|
||||
import org.booklore.model.websocket.Topic;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Join;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
@@ -12,7 +18,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -60,10 +65,16 @@ public class NotificationService {
|
||||
}
|
||||
|
||||
private List<String> findUsernamesWithPermissions(Set<PermissionType> permissionTypes) {
|
||||
String conditions = permissionTypes.stream()
|
||||
.map(p -> "p." + p.getEntityField() + " = true")
|
||||
.collect(Collectors.joining(" OR "));
|
||||
String jpql = "SELECT u.username FROM BookLoreUserEntity u JOIN u.permissions p WHERE " + conditions;
|
||||
return entityManager.createQuery(jpql, String.class).getResultList();
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<String> query = cb.createQuery(String.class);
|
||||
Root<BookLoreUserEntity> user = query.from(BookLoreUserEntity.class);
|
||||
Join<?, ?> perms = user.join("permissions");
|
||||
|
||||
Predicate[] predicates = permissionTypes.stream()
|
||||
.map(p -> cb.isTrue(perms.get(p.getEntityField())))
|
||||
.toArray(Predicate[]::new);
|
||||
|
||||
query.select(user.get("username")).where(cb.or(predicates));
|
||||
return entityManager.createQuery(query).getResultList();
|
||||
}
|
||||
}
|
||||
|
||||
+11
-27
@@ -15,6 +15,7 @@ import org.booklore.util.UserPermissionUtils;
|
||||
import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitialization;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -23,7 +24,6 @@ import tools.jackson.core.type.TypeReference;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -35,9 +35,6 @@ public class AppSettingService {
|
||||
private final AuthenticationService authenticationService;
|
||||
private final AuditService auditService;
|
||||
|
||||
private volatile AppSettings appSettings;
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
public AppSettingService(AppProperties appProperties, SettingPersistenceHelper settingPersistenceHelper, @Lazy AuthenticationService authenticationService, @Lazy AuditService auditService) {
|
||||
this.appProperties = appProperties;
|
||||
this.settingPersistenceHelper = settingPersistenceHelper;
|
||||
@@ -45,21 +42,15 @@ public class AppSettingService {
|
||||
this.auditService = auditService;
|
||||
}
|
||||
|
||||
@Cacheable("appSettings")
|
||||
public AppSettings getAppSettings() {
|
||||
if (appSettings == null) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (appSettings == null) {
|
||||
appSettings = buildAppSettings();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
return appSettings;
|
||||
return buildAppSettings();
|
||||
}
|
||||
|
||||
@CacheEvict(value = "publicSettings", allEntries = true)
|
||||
@Caching(evict = {
|
||||
@CacheEvict(value = "appSettings", allEntries = true),
|
||||
@CacheEvict(value = "publicSettings", allEntries = true)
|
||||
})
|
||||
@Transactional
|
||||
public void updateSetting(AppSettingKey key, Object val) throws JacksonException {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
@@ -77,7 +68,6 @@ public class AppSettingService {
|
||||
}
|
||||
setting.setVal(settingPersistenceHelper.serializeSettingValue(key, val));
|
||||
settingPersistenceHelper.appSettingsRepository.save(setting);
|
||||
refreshCache();
|
||||
|
||||
AuditAction action = switch (key) {
|
||||
case AppSettingKey k when k == AppSettingKey.OIDC_FORCE_ONLY_MODE -> AuditAction.OIDC_FORCE_ONLY_MODE_CHANGED;
|
||||
@@ -122,15 +112,6 @@ public class AppSettingService {
|
||||
return buildPublicSetting();
|
||||
}
|
||||
|
||||
private void refreshCache() {
|
||||
lock.lock();
|
||||
try {
|
||||
appSettings = buildAppSettings();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> getSettingsMap() {
|
||||
return settingPersistenceHelper.appSettingsRepository.findAll().stream()
|
||||
.filter(entity -> entity.getName() != null && entity.getVal() != null)
|
||||
@@ -217,7 +198,10 @@ public class AppSettingService {
|
||||
return setting != null ? setting.getVal() : null;
|
||||
}
|
||||
|
||||
@CacheEvict(value = "publicSettings", allEntries = true)
|
||||
@Caching(evict = {
|
||||
@CacheEvict(value = "appSettings", allEntries = true),
|
||||
@CacheEvict(value = "publicSettings", allEntries = true)
|
||||
})
|
||||
@Transactional
|
||||
public void saveSetting(String key, String value) {
|
||||
var setting = settingPersistenceHelper.appSettingsRepository.findByName(key);
|
||||
|
||||
Reference in New Issue
Block a user