mirror of https://github.com/halo-dev/halo
parent
7b88fcab5f
commit
92503e3822
|
@ -1,5 +1,7 @@
|
||||||
package run.halo.app.listener.freemarker;
|
package run.halo.app.listener.freemarker;
|
||||||
|
|
||||||
|
import static run.halo.app.model.support.HaloConst.OPTIONS_CACHE_KEY;
|
||||||
|
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.TemplateModelException;
|
import freemarker.template.TemplateModelException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -8,6 +10,7 @@ import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import run.halo.app.cache.AbstractStringCacheStore;
|
||||||
import run.halo.app.event.options.OptionUpdatedEvent;
|
import run.halo.app.event.options.OptionUpdatedEvent;
|
||||||
import run.halo.app.event.theme.ThemeActivatedEvent;
|
import run.halo.app.event.theme.ThemeActivatedEvent;
|
||||||
import run.halo.app.event.theme.ThemeUpdatedEvent;
|
import run.halo.app.event.theme.ThemeUpdatedEvent;
|
||||||
|
@ -41,16 +44,20 @@ public class FreemarkerConfigAwareListener {
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
|
private final AbstractStringCacheStore cacheStore;
|
||||||
|
|
||||||
public FreemarkerConfigAwareListener(OptionService optionService,
|
public FreemarkerConfigAwareListener(OptionService optionService,
|
||||||
Configuration configuration,
|
Configuration configuration,
|
||||||
ThemeService themeService,
|
ThemeService themeService,
|
||||||
ThemeSettingService themeSettingService,
|
ThemeSettingService themeSettingService,
|
||||||
UserService userService) {
|
UserService userService,
|
||||||
|
AbstractStringCacheStore cacheStore) {
|
||||||
this.optionService = optionService;
|
this.optionService = optionService;
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
this.themeService = themeService;
|
this.themeService = themeService;
|
||||||
this.themeSettingService = themeSettingService;
|
this.themeSettingService = themeSettingService;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
this.cacheStore = cacheStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventListener
|
@EventListener
|
||||||
|
@ -90,6 +97,10 @@ public class FreemarkerConfigAwareListener {
|
||||||
public void onOptionUpdate(OptionUpdatedEvent event) throws TemplateModelException {
|
public void onOptionUpdate(OptionUpdatedEvent event) throws TemplateModelException {
|
||||||
log.debug("Received option updated event");
|
log.debug("Received option updated event");
|
||||||
|
|
||||||
|
// refresh options cache
|
||||||
|
optionService.flush();
|
||||||
|
cacheStore.delete(OPTIONS_CACHE_KEY);
|
||||||
|
|
||||||
loadOptionsConfig();
|
loadOptionsConfig();
|
||||||
loadThemeConfig();
|
loadThemeConfig();
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,6 +155,11 @@ public class HaloConst {
|
||||||
*/
|
*/
|
||||||
public static String DATABASE_PRODUCT_NAME = null;
|
public static String DATABASE_PRODUCT_NAME = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options cache key.
|
||||||
|
*/
|
||||||
|
public static String OPTIONS_CACHE_KEY = "options";
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Set version
|
// Set version
|
||||||
HALO_VERSION = Optional.ofNullable(HaloConst.class.getPackage().getImplementationVersion())
|
HALO_VERSION = Optional.ofNullable(HaloConst.class.getPackage().getImplementationVersion())
|
||||||
|
|
|
@ -13,10 +13,12 @@ import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import run.halo.app.config.properties.HaloProperties;
|
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.AlreadyExistsException;
|
||||||
import run.halo.app.exception.NotFoundException;
|
import run.halo.app.exception.NotFoundException;
|
||||||
import run.halo.app.exception.ServiceException;
|
import run.halo.app.exception.ServiceException;
|
||||||
|
@ -41,10 +43,14 @@ public class ThemeRepositoryImpl implements ThemeRepository {
|
||||||
|
|
||||||
private final HaloProperties properties;
|
private final HaloProperties properties;
|
||||||
|
|
||||||
|
private final ApplicationEventPublisher eventPublisher;
|
||||||
|
|
||||||
public ThemeRepositoryImpl(OptionRepository optionRepository,
|
public ThemeRepositoryImpl(OptionRepository optionRepository,
|
||||||
HaloProperties properties) {
|
HaloProperties properties,
|
||||||
|
ApplicationEventPublisher eventPublisher) {
|
||||||
this.optionRepository = optionRepository;
|
this.optionRepository = optionRepository;
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
|
this.eventPublisher = eventPublisher;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,7 +80,6 @@ public class ThemeRepositoryImpl implements ThemeRepository {
|
||||||
@Override
|
@Override
|
||||||
public void setActivatedTheme(@NonNull String themeId) {
|
public void setActivatedTheme(@NonNull String themeId) {
|
||||||
Assert.hasText(themeId, "Theme id must not be blank");
|
Assert.hasText(themeId, "Theme id must not be blank");
|
||||||
|
|
||||||
final var newThemeOption = optionRepository.findByKey(PrimaryProperties.THEME.getValue())
|
final var newThemeOption = optionRepository.findByKey(PrimaryProperties.THEME.getValue())
|
||||||
.map(themeOption -> {
|
.map(themeOption -> {
|
||||||
// set theme id
|
// set theme id
|
||||||
|
@ -83,6 +88,8 @@ public class ThemeRepositoryImpl implements ThemeRepository {
|
||||||
})
|
})
|
||||||
.orElseGet(() -> new Option(PrimaryProperties.THEME.getValue(), themeId));
|
.orElseGet(() -> new Option(PrimaryProperties.THEME.getValue(), themeId));
|
||||||
optionRepository.save(newThemeOption);
|
optionRepository.save(newThemeOption);
|
||||||
|
|
||||||
|
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -136,7 +136,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
|
||||||
if (!CollectionUtils.isEmpty(optionsToUpdate)
|
if (!CollectionUtils.isEmpty(optionsToUpdate)
|
||||||
|| !CollectionUtils.isEmpty(optionsToCreate)) {
|
|| !CollectionUtils.isEmpty(optionsToCreate)) {
|
||||||
// If there is something changed
|
// 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) {
|
public void save(OptionParam optionParam) {
|
||||||
Option option = optionParam.convertTo();
|
Option option = optionParam.convertTo();
|
||||||
create(option);
|
create(option);
|
||||||
publishOptionUpdatedEvent();
|
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -164,7 +164,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
|
||||||
Option optionToUpdate = getById(optionId);
|
Option optionToUpdate = getById(optionId);
|
||||||
optionParam.update(optionToUpdate);
|
optionParam.update(optionToUpdate);
|
||||||
update(optionToUpdate);
|
update(optionToUpdate);
|
||||||
publishOptionUpdatedEvent();
|
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -271,7 +271,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
|
||||||
@Override
|
@Override
|
||||||
public Option removePermanently(Integer id) {
|
public Option removePermanently(Integer id) {
|
||||||
Option deletedOption = removeById(id);
|
Option deletedOption = removeById(id);
|
||||||
publishOptionUpdatedEvent();
|
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
|
||||||
return deletedOption;
|
return deletedOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -629,7 +629,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
|
||||||
replaced.add(option);
|
replaced.add(option);
|
||||||
});
|
});
|
||||||
List<Option> updated = updateInBatch(replaced);
|
List<Option> updated = updateInBatch(replaced);
|
||||||
publishOptionUpdatedEvent();
|
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
|
||||||
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
|
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);
|
return new OptionSimpleDTO().convertFrom(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
private void cleanCache() {
|
private void cleanCache() {
|
||||||
cacheStore.delete(OPTIONS_KEY);
|
cacheStore.delete(OPTIONS_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
private void publishOptionUpdatedEvent() {
|
private void publishOptionUpdatedEvent() {
|
||||||
flush();
|
flush();
|
||||||
cleanCache();
|
cleanCache();
|
||||||
|
|
Loading…
Reference in New Issue