Skip to content

Commit

Permalink
Cache current theme in theme repository (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnNiang authored Feb 25, 2021
1 parent 29466c5 commit 65ed8b8
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 9 deletions.
55 changes: 46 additions & 9 deletions src/main/java/run/halo/app/repository/ThemeRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package run.halo.app.repository;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static run.halo.app.model.properties.PrimaryProperties.THEME;
import static run.halo.app.model.support.HaloConst.DEFAULT_THEME_ID;
import static run.halo.app.utils.FileUtils.copyFolder;
import static run.halo.app.utils.FileUtils.deleteFolderQuietly;
import static run.halo.app.utils.VersionUtil.compareVersion;
Expand All @@ -14,6 +16,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
Expand All @@ -25,7 +28,6 @@
import run.halo.app.exception.ThemeNotSupportException;
import run.halo.app.handler.theme.config.support.ThemeProperty;
import run.halo.app.model.entity.Option;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.HaloConst;
import run.halo.app.theme.ThemePropertyScanner;
import run.halo.app.utils.FileUtils;
Expand All @@ -37,14 +39,17 @@
*/
@Repository
@Slf4j
public class ThemeRepositoryImpl implements ThemeRepository {
public class ThemeRepositoryImpl
implements ThemeRepository, ApplicationListener<OptionUpdatedEvent> {

private final OptionRepository optionRepository;

private final HaloProperties properties;

private final ApplicationEventPublisher eventPublisher;

private volatile ThemeProperty currentTheme;

public ThemeRepositoryImpl(OptionRepository optionRepository,
HaloProperties properties,
ApplicationEventPublisher eventPublisher) {
Expand All @@ -55,19 +60,35 @@ public ThemeRepositoryImpl(OptionRepository optionRepository,

@Override
public String getActivatedThemeId() {
return optionRepository.findByKey(PrimaryProperties.THEME.getValue())
.map(Option::getValue)
.orElse(HaloConst.DEFAULT_THEME_ID);
return getActivatedThemeProperty().getId();
}

@Override
public ThemeProperty getActivatedThemeProperty() {
return fetchThemePropertyByThemeId(getActivatedThemeId()).orElseThrow();
ThemeProperty themeProperty = this.currentTheme;
if (themeProperty == null) {
synchronized (this) {
if (this.currentTheme == null) {
// get current theme id
String currentThemeId = this.optionRepository.findByKey(THEME.getValue())
.map(Option::getValue)
.orElse(DEFAULT_THEME_ID);
// fetch current theme
this.currentTheme = this.getThemeByThemeId(currentThemeId);
}
}
}
return this.currentTheme;
}

@Override
public Optional<ThemeProperty> fetchThemePropertyByThemeId(String themeId) {
return listAll().stream()
if (StringUtils.equals(themeId, getActivatedThemeId())) {
return Optional.of(getActivatedThemeProperty());
}

return ThemePropertyScanner.INSTANCE.scan(getThemeRootPath(), null)
.stream()
.filter(property -> Objects.equals(themeId, property.getId()))
.findFirst();
}
Expand All @@ -80,13 +101,13 @@ public List<ThemeProperty> listAll() {
@Override
public void setActivatedTheme(@NonNull String themeId) {
Assert.hasText(themeId, "Theme id must not be blank");
final var newThemeOption = optionRepository.findByKey(PrimaryProperties.THEME.getValue())
final var newThemeOption = optionRepository.findByKey(THEME.getValue())
.map(themeOption -> {
// set theme id
themeOption.setValue(themeId);
return themeOption;
})
.orElseGet(() -> new Option(PrimaryProperties.THEME.getValue(), themeId));
.orElseGet(() -> new Option(THEME.getValue(), themeId));
optionRepository.save(newThemeOption);

eventPublisher.publishEvent(new OptionUpdatedEvent(this));
Expand Down Expand Up @@ -163,4 +184,20 @@ public boolean checkThemePropertyCompatibility(ThemeProperty themeProperty) {
private Path getThemeRootPath() {
return Paths.get(properties.getWorkDir()).resolve("templates/themes");
}

@Override
public void onApplicationEvent(OptionUpdatedEvent event) {
synchronized (this) {
this.currentTheme = null;
}
}

@NonNull
protected ThemeProperty getThemeByThemeId(String themeId) {
return ThemePropertyScanner.INSTANCE.scan(getThemeRootPath(), null)
.stream()
.filter(property -> Objects.equals(themeId, property.getId()))
.findFirst()
.orElseThrow();
}
}
102 changes: 102 additions & 0 deletions src/test/java/run/halo/app/repository/ThemeRepositoryImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package run.halo.app.repository;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static run.halo.app.model.properties.PrimaryProperties.THEME;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.context.ApplicationEventPublisher;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.handler.theme.config.support.ThemeProperty;
import run.halo.app.model.support.HaloConst;

/**
* Theme repository impl test.
*
* @author johnniang
*/
class ThemeRepositoryImplTest {

@InjectMocks
@Spy
ThemeRepositoryImpl themeRepository;

@Mock
OptionRepository optionRepository;

@Mock
HaloProperties haloProperties;

@Mock
ApplicationEventPublisher eventPublisher;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void getActivatedThemeBySingleThread() {
ThemeProperty expectedTheme = new ThemeProperty();
expectedTheme.setId(HaloConst.DEFAULT_THEME_ID);
expectedTheme.setActivated(true);

given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
doReturn(expectedTheme).when(themeRepository)
.getThemeByThemeId(HaloConst.DEFAULT_THEME_ID);

ThemeProperty resultTheme = themeRepository.getActivatedThemeProperty();
assertEquals(expectedTheme, resultTheme);

verify(optionRepository, times(1)).findByKey(any());
verify(themeRepository, times(1)).getThemeByThemeId(any());
}

@Test
void getActivatedThemeByMultiThread() throws InterruptedException {
ThemeProperty expectedTheme = new ThemeProperty();
expectedTheme.setId(HaloConst.DEFAULT_THEME_ID);
expectedTheme.setActivated(true);

given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
doReturn(expectedTheme).when(themeRepository)
.getThemeByThemeId(HaloConst.DEFAULT_THEME_ID);

ExecutorService executorService = Executors.newFixedThreadPool(10);
// define tasks
List<Callable<ThemeProperty>> tasks = IntStream.range(0, 10)
.mapToObj(
i -> (Callable<ThemeProperty>) () -> themeRepository.getActivatedThemeProperty())
.collect(Collectors.toList());

// invoke and get results
executorService.invokeAll(tasks).forEach(future -> {
try {
assertEquals(expectedTheme, future.get(100, TimeUnit.MILLISECONDS));
} catch (Exception e) {
throw new RuntimeException("Failed to get task result!", e);
}
});

verify(optionRepository, times(1)).findByKey(any());
verify(themeRepository, times(1)).getThemeByThemeId(any());
}

}

0 comments on commit 65ed8b8

Please sign in to comment.