diff --git a/src/main/java/run/halo/app/listener/freemarker/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/listener/freemarker/FreemarkerConfigAwareListener.java index c8d12f1c9..471102133 100644 --- a/src/main/java/run/halo/app/listener/freemarker/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/listener/freemarker/FreemarkerConfigAwareListener.java @@ -1,5 +1,7 @@ package run.halo.app.listener.freemarker; +import static run.halo.app.model.support.HaloConst.OPTIONS_CACHE_KEY; + import freemarker.template.Configuration; import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; @@ -8,6 +10,7 @@ import org.springframework.context.event.EventListener; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import run.halo.app.cache.AbstractStringCacheStore; import run.halo.app.event.options.OptionUpdatedEvent; import run.halo.app.event.theme.ThemeActivatedEvent; import run.halo.app.event.theme.ThemeUpdatedEvent; @@ -41,16 +44,20 @@ public class FreemarkerConfigAwareListener { private final UserService userService; + private final AbstractStringCacheStore cacheStore; + public FreemarkerConfigAwareListener(OptionService optionService, Configuration configuration, ThemeService themeService, ThemeSettingService themeSettingService, - UserService userService) { + UserService userService, + AbstractStringCacheStore cacheStore) { this.optionService = optionService; this.configuration = configuration; this.themeService = themeService; this.themeSettingService = themeSettingService; this.userService = userService; + this.cacheStore = cacheStore; } @EventListener @@ -90,6 +97,10 @@ public class FreemarkerConfigAwareListener { public void onOptionUpdate(OptionUpdatedEvent event) throws TemplateModelException { log.debug("Received option updated event"); + // refresh options cache + optionService.flush(); + cacheStore.delete(OPTIONS_CACHE_KEY); + loadOptionsConfig(); loadThemeConfig(); } diff --git a/src/main/java/run/halo/app/model/support/HaloConst.java b/src/main/java/run/halo/app/model/support/HaloConst.java index 93ce573e6..097d5080f 100644 --- a/src/main/java/run/halo/app/model/support/HaloConst.java +++ b/src/main/java/run/halo/app/model/support/HaloConst.java @@ -155,6 +155,11 @@ public class HaloConst { */ public static String DATABASE_PRODUCT_NAME = null; + /** + * Options cache key. + */ + public static String OPTIONS_CACHE_KEY = "options"; + static { // Set version HALO_VERSION = Optional.ofNullable(HaloConst.class.getPackage().getImplementationVersion()) diff --git a/src/main/java/run/halo/app/repository/ThemeRepositoryImpl.java b/src/main/java/run/halo/app/repository/ThemeRepositoryImpl.java index 9eb98b9e6..6e4c48e60 100644 --- a/src/main/java/run/halo/app/repository/ThemeRepositoryImpl.java +++ b/src/main/java/run/halo/app/repository/ThemeRepositoryImpl.java @@ -13,10 +13,12 @@ import java.util.Objects; import java.util.Optional; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.lang.NonNull; import org.springframework.stereotype.Repository; import org.springframework.util.Assert; import run.halo.app.config.properties.HaloProperties; +import run.halo.app.event.options.OptionUpdatedEvent; import run.halo.app.exception.AlreadyExistsException; import run.halo.app.exception.NotFoundException; import run.halo.app.exception.ServiceException; @@ -41,10 +43,14 @@ public class ThemeRepositoryImpl implements ThemeRepository { private final HaloProperties properties; + private final ApplicationEventPublisher eventPublisher; + public ThemeRepositoryImpl(OptionRepository optionRepository, - HaloProperties properties) { + HaloProperties properties, + ApplicationEventPublisher eventPublisher) { this.optionRepository = optionRepository; this.properties = properties; + this.eventPublisher = eventPublisher; } @Override @@ -74,7 +80,6 @@ public class ThemeRepositoryImpl implements ThemeRepository { @Override public void setActivatedTheme(@NonNull String themeId) { Assert.hasText(themeId, "Theme id must not be blank"); - final var newThemeOption = optionRepository.findByKey(PrimaryProperties.THEME.getValue()) .map(themeOption -> { // set theme id @@ -83,6 +88,8 @@ public class ThemeRepositoryImpl implements ThemeRepository { }) .orElseGet(() -> new Option(PrimaryProperties.THEME.getValue(), themeId)); optionRepository.save(newThemeOption); + + eventPublisher.publishEvent(new OptionUpdatedEvent(this)); } @Override diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index 580fdcd57..ea6ccc2a6 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -136,7 +136,7 @@ public class OptionServiceImpl extends AbstractCrudService if (!CollectionUtils.isEmpty(optionsToUpdate) || !CollectionUtils.isEmpty(optionsToCreate)) { // If there is something changed - publishOptionUpdatedEvent(); + eventPublisher.publishEvent(new OptionUpdatedEvent(this)); } } @@ -156,7 +156,7 @@ public class OptionServiceImpl extends AbstractCrudService public void save(OptionParam optionParam) { Option option = optionParam.convertTo(); create(option); - publishOptionUpdatedEvent(); + eventPublisher.publishEvent(new OptionUpdatedEvent(this)); } @Override @@ -164,7 +164,7 @@ public class OptionServiceImpl extends AbstractCrudService Option optionToUpdate = getById(optionId); optionParam.update(optionToUpdate); update(optionToUpdate); - publishOptionUpdatedEvent(); + eventPublisher.publishEvent(new OptionUpdatedEvent(this)); } @Override @@ -271,7 +271,7 @@ public class OptionServiceImpl extends AbstractCrudService @Override public Option removePermanently(Integer id) { Option deletedOption = removeById(id); - publishOptionUpdatedEvent(); + eventPublisher.publishEvent(new OptionUpdatedEvent(this)); return deletedOption; } @@ -629,7 +629,7 @@ public class OptionServiceImpl extends AbstractCrudService replaced.add(option); }); List