未验证 提交 92503e38 编写于 作者: RYAN0UP's avatar RYAN0UP 提交者: GitHub

fix: #1255. (#1256)

* fix: #1255.

* fix: #1255.
上级 7b88fcab
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();
}
......
......@@ -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())
......
......@@ -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
......
......@@ -136,7 +136,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
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<Option, Integer>
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, Integer>
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<Option, Integer>
@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<Option, Integer>
replaced.add(option);
});
List<Option> updated = updateInBatch(replaced);
publishOptionUpdatedEvent();
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
}
......@@ -640,10 +640,12 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
return new OptionSimpleDTO().convertFrom(option);
}
@Deprecated
private void cleanCache() {
cacheStore.delete(OPTIONS_KEY);
}
@Deprecated
private void publishOptionUpdatedEvent() {
flush();
cleanCache();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册