forked from halo-dev/plugin-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package run.halo.sitemap; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import java.time.Duration; | ||
import lombok.AllArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
@Component | ||
@AllArgsConstructor | ||
public class CachedSitemapGetter { | ||
|
||
private final Cache<String, String> cache = CacheBuilder.newBuilder() | ||
.concurrencyLevel(Runtime.getRuntime().availableProcessors()) | ||
.initialCapacity(8) | ||
.maximumSize(8) | ||
.expireAfterWrite(Duration.ofSeconds(30)) | ||
.build(); | ||
|
||
private final DefaultSitemapEntryLister lister; | ||
|
||
public Mono<String> get() { | ||
String cacheKey = "sitemap"; | ||
return Mono.fromCallable(() -> cache.get(cacheKey, () -> lister.list() | ||
.collectList() | ||
.map(entries -> { | ||
String xml = new SitemapBuilder() | ||
.buildSitemapXml(entries); | ||
cache.put(cacheKey, xml); | ||
return xml; | ||
}) | ||
.defaultIfEmpty(StringUtils.EMPTY) | ||
.block() | ||
)) | ||
.subscribeOn(Schedulers.boundedElastic()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
src/main/java/run/halo/sitemap/DefaultSitemapXmlSupplier.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/java/run/halo/sitemap/CachedSitemapGetterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package run.halo.sitemap; | ||
|
||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import reactor.core.publisher.Flux; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class CachedSitemapGetterTest { | ||
@Mock | ||
private DefaultSitemapEntryLister lister; | ||
private CachedSitemapGetter getter; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(lister.list()).thenReturn( | ||
Flux.just(SitemapEntry.builder().loc("http://localhost:8090/about").build())); | ||
getter = new CachedSitemapGetter(lister); | ||
} | ||
|
||
@Test | ||
void get() throws InterruptedException { | ||
CountDownLatch countDownLatch = new CountDownLatch(10); | ||
|
||
for (int i = 0; i < countDownLatch.getCount(); i++) { | ||
new Thread(() -> { | ||
getter.get().block(); | ||
countDownLatch.countDown(); | ||
}).start(); | ||
} | ||
countDownLatch.await(); | ||
verify(lister, times(1)).list(); | ||
} | ||
} |