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