diff --git a/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java index a8147b814..34f9f435b 100644 --- a/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java @@ -38,21 +38,17 @@ public class FreemarkerConfigAwareListener { private final ThemeSettingService themeSettingService; - private final OptionService optionsService; - private final UserService userService; public FreemarkerConfigAwareListener(OptionService optionService, Configuration configuration, ThemeService themeService, ThemeSettingService themeSettingService, - OptionService optionsService, UserService userService) { this.optionService = optionService; this.configuration = configuration; this.themeService = themeService; this.themeSettingService = themeSettingService; - this.optionsService = optionsService; this.userService = userService; } @@ -97,13 +93,11 @@ public class FreemarkerConfigAwareListener { } private void loadOptionsConfig() throws TemplateModelException { - Map options = optionService.listOptions(); - configuration.setSharedVariable("options", options); + configuration.setSharedVariable("options", optionService.listOptions()); } private void loadThemeConfig() throws TemplateModelException { - ThemeProperty activatedTheme = themeService.getActivatedTheme(); - configuration.setSharedVariable("theme", activatedTheme); + configuration.setSharedVariable("theme", themeService.getActivatedTheme()); configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); } } diff --git a/src/main/java/run/halo/app/event/options/OptionUpdatedListener.java b/src/main/java/run/halo/app/event/options/OptionUpdatedListener.java new file mode 100644 index 000000000..aeb06e1c1 --- /dev/null +++ b/src/main/java/run/halo/app/event/options/OptionUpdatedListener.java @@ -0,0 +1,29 @@ +package run.halo.app.event.options; + +import org.springframework.context.ApplicationListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import run.halo.app.cache.StringCacheStore; +import run.halo.app.service.OptionService; + +/** + * Option updated listener. + * + * @author johnniang + * @date 19-4-29 + */ +@Component +public class OptionUpdatedListener implements ApplicationListener { + + private final StringCacheStore cacheStore; + + public OptionUpdatedListener(StringCacheStore cacheStore) { + this.cacheStore = cacheStore; + } + + @Override + @Async + public void onApplicationEvent(OptionUpdatedEvent event) { + cacheStore.delete(OptionService.OPTIONS_KEY); + } +} diff --git a/src/main/java/run/halo/app/event/theme/ThemeUpdatedEvent.java b/src/main/java/run/halo/app/event/theme/ThemeUpdatedEvent.java new file mode 100644 index 000000000..6c460e4d7 --- /dev/null +++ b/src/main/java/run/halo/app/event/theme/ThemeUpdatedEvent.java @@ -0,0 +1,21 @@ +package run.halo.app.event.theme; + +import org.springframework.context.ApplicationEvent; + +/** + * Theme updated event. + * + * @author johnniang + * @date 19-4-29 + */ +public class ThemeUpdatedEvent extends ApplicationEvent { + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public ThemeUpdatedEvent(Object source) { + super(source); + } +} diff --git a/src/main/java/run/halo/app/event/theme/ThemeUpdatedListener.java b/src/main/java/run/halo/app/event/theme/ThemeUpdatedListener.java new file mode 100644 index 000000000..34f164cff --- /dev/null +++ b/src/main/java/run/halo/app/event/theme/ThemeUpdatedListener.java @@ -0,0 +1,29 @@ +package run.halo.app.event.theme; + +import org.springframework.context.ApplicationListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import run.halo.app.cache.StringCacheStore; +import run.halo.app.service.ThemeService; + +/** + * Theme updated listener. + * + * @author johnniang + * @date 19-4-29 + */ +@Component +public class ThemeUpdatedListener implements ApplicationListener { + + private final StringCacheStore cacheStore; + + public ThemeUpdatedListener(StringCacheStore cacheStore) { + this.cacheStore = cacheStore; + } + + @Override + @Async + public void onApplicationEvent(ThemeUpdatedEvent event) { + cacheStore.delete(ThemeService.THEMES_CACHE_KEY); + } +} diff --git a/src/main/java/run/halo/app/service/OptionService.java b/src/main/java/run/halo/app/service/OptionService.java index 7376886d7..34c08edf6 100755 --- a/src/main/java/run/halo/app/service/OptionService.java +++ b/src/main/java/run/halo/app/service/OptionService.java @@ -30,14 +30,7 @@ public interface OptionService extends CrudService { int DEFAULT_RSS_PAGE_SIZE = 20; - /** - * Save one option - * - * @param key key must not be blank - * @param value value - */ - @Transactional - void save(@NonNull String key, String value); + String OPTIONS_KEY = "options"; /** * Save multiple options 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 7fca348a4..43bc6af3b 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -19,7 +19,6 @@ import run.halo.app.model.params.OptionParam; import run.halo.app.model.properties.*; import run.halo.app.repository.OptionRepository; import run.halo.app.service.OptionService; -import run.halo.app.service.ThemeService; import run.halo.app.service.base.AbstractCrudService; import run.halo.app.utils.HaloUtils; import run.halo.app.utils.ServiceUtils; @@ -60,8 +59,7 @@ public class OptionServiceImpl extends AbstractCrudService impl propertyEnumMap = Collections.unmodifiableMap(PropertyEnum.getValuePropertyEnumMap()); } - @Override - public void save(String key, String value) { + private void save(String key, String value) { Assert.hasText(key, "Option key must not be blank"); if (StringUtils.isBlank(value)) { @@ -90,8 +88,6 @@ public class OptionServiceImpl extends AbstractCrudService impl Option savedOption = optionRepository.save(option); log.debug("Saved option: [{}]", savedOption); - - cacheStore.delete(ThemeService.THEMES_CACHE_KEY); } @Override @@ -103,7 +99,7 @@ public class OptionServiceImpl extends AbstractCrudService impl // TODO Optimize the queries options.forEach(this::save); - publishEvent(); + publishOptionUpdatedEvent(); } @Override @@ -115,7 +111,7 @@ public class OptionServiceImpl extends AbstractCrudService impl // TODO Optimize the query optionParams.forEach(optionParam -> save(optionParam.getKey(), optionParam.getValue())); - publishEvent(); + publishOptionUpdatedEvent(); } @Override @@ -124,7 +120,7 @@ public class OptionServiceImpl extends AbstractCrudService impl save(property.getValue(), value); - publishEvent(); + publishOptionUpdatedEvent(); } @Override @@ -135,44 +131,51 @@ public class OptionServiceImpl extends AbstractCrudService impl properties.forEach((property, value) -> save(property.getValue(), value)); - publishEvent(); + publishOptionUpdatedEvent(); } @Override + @SuppressWarnings("unchecked") public Map listOptions() { - List