Skip to content

Commit

Permalink
#226 Make ExtensionPoint for cache provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Aug 22, 2024
1 parent 5d4d4e3 commit 0606875
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public static class ContentTypes {

public static final int DEFAULT_REDIRECT_STATUS = 301;

public static final String DEFAULT_CACHE_ENGINE = "local";

public static class Taxonomy {
public static final String DEFAULT_TEMPLATE = "taxonomy.html";
public static final String DEFAULT_SINGLE_TEMPLATE = "taxonomy.single.html";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public Locale locale () {
public String defaultContentType () {
return (String)getSubMap("content").getOrDefault("type", Constants.DEFAULT_CONTENT_TYPE);
}

public String cacheEngine() {
return (String) getSubMap("cache").getOrDefault("engine", Constants.DEFAULT_CACHE_ENGINE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
/**
*
* @author t.marx
* @param <K>
* @param <V>
*/
public interface CacheProvider<K extends Serializable, V extends Serializable> {
public interface CacheProvider {

ICache<K, V> getCache (String name, CacheManager.CacheConfig config);
<K extends Serializable, V extends Serializable> ICache<K, V> getCache (String name, CacheManager.CacheConfig config);

ICache<K, V> getCache (String name, CacheManager.CacheConfig config, Function<K, V> loader);
<K extends Serializable, V extends Serializable> ICache<K, V> getCache (String name, CacheManager.CacheConfig config, Function<K, V> loader);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.thmarx.cms.api.extensions;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.cache.CacheProvider;

/**
*
* @author t.marx
*/
public abstract class CacheProviderExtentionPoint extends AbstractExtensionPoint {

public abstract String getName ();
public abstract CacheProvider getCacheProvider ();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.github.thmarx.cms.content;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.ServerContext;
import com.github.thmarx.cms.api.cache.CacheManager;
import com.github.thmarx.cms.api.cache.ICache;
import com.github.thmarx.cms.api.content.ContentParser;
import com.github.thmarx.cms.api.db.cms.ReadOnlyFile;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

/**
*
* @author t.marx
*/
@Slf4j
public class CachedContentParser implements com.github.thmarx.cms.api.content.ContentParser{

private final ContentParser defaultContentParser;
private final ICache<String, Content> contentCache;

@Inject
public CachedContentParser(final CacheManager cacheManager, final DefaultContentParser contentParser) {
this.defaultContentParser = contentParser;
if (ServerContext.IS_DEV) {
contentCache = cacheManager.get("contentCache",
new CacheManager.CacheConfig(10l, Duration.ofMinutes(1)));
} else {
contentCache = cacheManager.get("contentCache",
new CacheManager.CacheConfig(0l, Duration.ofMinutes(1)));
}
}

@Override
public void clearCache() {
contentCache.invalidate();
}

@Override
public Content parse(final ReadOnlyFile contentFile) throws IOException {
final String filename = contentFile.toAbsolutePath().toString();
var cached = contentCache.get(filename);
if (cached != null) {
return cached;
}
var object = defaultContentParser.parse(contentFile);
contentCache.put(filename, object);
return object;
}



@Override
public Map<String, Object> parseMeta(final ReadOnlyFile contentFile) throws IOException {
return defaultContentParser.parseMeta(contentFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
* #L%
*/

import com.github.thmarx.cms.api.ServerContext;
import com.github.thmarx.cms.api.cache.CacheManager;
import com.github.thmarx.cms.api.cache.ICache;
import com.github.thmarx.cms.api.content.ContentParser;
import com.github.thmarx.cms.api.db.cms.ReadOnlyFile;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -42,38 +38,19 @@
* @author t.marx
*/
@Slf4j
public class DefaultContentParser implements com.github.thmarx.cms.api.content.ContentParser{
public class DefaultContentParser implements ContentParser{

private final ICache<String, Content> contentCache;

@Inject
public DefaultContentParser(final CacheManager cacheManager) {
if (ServerContext.IS_DEV) {
contentCache = cacheManager.get("contentCache",
new CacheManager.CacheConfig(10l, Duration.ofMinutes(1)));
} else {
contentCache = cacheManager.get("contentCache",
new CacheManager.CacheConfig(0l, Duration.ofMinutes(1)));
}

public DefaultContentParser() {
}

@Override
public void clearCache() {
contentCache.invalidate();

}

@Override
public Content parse(final ReadOnlyFile contentFile) throws IOException {
final String filename = contentFile.toAbsolutePath().toString();
var cached = contentCache.get(filename);
if (cached != null) {
return cached;
}
var object = _parse(contentFile);
contentCache.put(filename, object);
return object;
}

private Content _parse(final ReadOnlyFile contentFile) throws IOException {
ContentRecord readContent = readContent(contentFile);

return new Content(readContent.content(), _parseMeta(readContent));
Expand All @@ -91,6 +68,7 @@ private Map<String, Object> _parseMeta (ContentRecord content) {
}
}

@Override
public Map<String, Object> parseMeta(final ReadOnlyFile contentFile) throws IOException {
ContentRecord readContent = readContent(contentFile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
*
* @author t.marx
*/
public class LocalCacheProvider<K extends Serializable, V extends Serializable> implements CacheProvider<K, V> {
public class LocalCacheProvider implements CacheProvider {

private final ConcurrentMap<String, ICache<K, V>> caches = new ConcurrentHashMap<>();
private final ConcurrentMap<String, ICache> caches = new ConcurrentHashMap<>();

private Cache<K,V> buildCache (CacheManager.CacheConfig config) {
private <K extends Serializable, V extends Serializable> Cache<K,V> buildCache (CacheManager.CacheConfig config) {
var cache = Caffeine.newBuilder();

if (config.maxSize() != null) {
Expand All @@ -55,7 +55,7 @@ private Cache<K,V> buildCache (CacheManager.CacheConfig config) {
}

@Override
public ICache<K, V> getCache(String name, CacheManager.CacheConfig config) {
public <K extends Serializable, V extends Serializable> ICache<K, V> getCache(String name, CacheManager.CacheConfig config) {
if (!caches.containsKey(name)) {
caches.putIfAbsent(name, new LocalCache(
buildCache(config),
Expand All @@ -65,7 +65,7 @@ public ICache<K, V> getCache(String name, CacheManager.CacheConfig config) {
}

@Override
public ICache<K, V> getCache(String name, CacheManager.CacheConfig config, Function<K, V> loader) {
public <K extends Serializable, V extends Serializable> ICache<K, V> getCache(String name, CacheManager.CacheConfig config, Function<K, V> loader) {
if (!caches.containsKey(name)) {
caches.putIfAbsent(name, new LocalCache(
buildCache(config),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.cache.CacheManager;
import com.github.thmarx.cms.api.cache.CacheProvider;
import com.github.thmarx.cms.api.extensions.CacheProviderExtentionPoint;
import com.github.thmarx.cms.api.hooks.HookSystem;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import com.github.thmarx.cms.core.cache.LocalCacheProvider;
import com.github.thmarx.cms.core.scheduler.SingleCronJobScheduler;
import com.github.thmarx.cms.core.scheduler.SiteCronJobScheduler;
import com.github.thmarx.cms.extensions.GlobalExtensions;
import com.github.thmarx.cms.extensions.hooks.GlobalHooks;
import com.github.thmarx.modules.api.ModuleManager;
import com.google.inject.Binder;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
Expand Down Expand Up @@ -97,7 +104,23 @@ public SingleCronJobScheduler singleCronJobScheduler (Scheduler scheduler, CronJ

@Provides
@Singleton
public CacheManager cacheManager () {
return new CacheManager(new LocalCacheProvider());
public CacheManager cacheManager (CacheProvider cacheProvider) {
return new CacheManager(cacheProvider);
}

@Provides
@Singleton
public CacheProvider cacheProvider (ModuleManager moduleManager, SiteProperties siteProperties) {
var cacheEngine = siteProperties.cacheEngine();
if (Constants.DEFAULT_CACHE_ENGINE.equals(cacheEngine)) {
return new LocalCacheProvider();
}
List<CacheProviderExtentionPoint> extensions = moduleManager.extensions(CacheProviderExtentionPoint.class);
Optional<CacheProviderExtentionPoint> extOpt = extensions.stream().filter((ext) -> ext.getName().equals(cacheEngine)).findFirst();

if (extOpt.isPresent()) {
return extOpt.get().getCacheProvider();
}
return new LocalCacheProvider();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.auth.services.AuthService;
import com.github.thmarx.cms.auth.services.UserService;
import com.github.thmarx.cms.content.CachedContentParser;
import com.github.thmarx.cms.content.ContentRenderer;
import com.github.thmarx.cms.content.ContentResolver;
import com.github.thmarx.cms.content.DefaultContentParser;
Expand Down Expand Up @@ -94,7 +95,8 @@ protected void configure() {
bind(Configuration.class).toInstance(configuration);
bind(ScheduledExecutorService.class).toInstance(scheduledExecutorService);
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);
bind(ContentParser.class).to(DefaultContentParser.class).in(Singleton.class);
bind(DefaultContentParser.class).in(Singleton.class);
bind(ContentParser.class).to(CachedContentParser.class).in(Singleton.class);
bind(TaxonomyFunction.class).in(Singleton.class);
bind(ContentNodeMapper.class).in(Singleton.class);
bind(TaxonomyResolver.class).in(Singleton.class);
Expand Down Expand Up @@ -169,7 +171,7 @@ public MessageSource messages(SiteProperties site, DB db) throws IOException {

@Provides
@Singleton
public DB fileDb(SiteProperties site, ContentParser contentParser, Configuration configuration, EventBus eventBus) throws IOException {
public DB fileDb(SiteProperties site, DefaultContentParser contentParser, Configuration configuration, EventBus eventBus) throws IOException {
var db = new FileDB(hostBase, eventBus, (file) -> {
try {
ReadOnlyFile cmsFile = new NIOReadOnlyFile(file, hostBase.resolve(Constants.Folders.CONTENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class SectionsTest extends TemplateEngineTest {

@BeforeAll
public static void beforeClass() throws IOException {
var contentParser = new DefaultContentParser(new CacheManager(new LocalCacheProvider()));
var contentParser = new DefaultContentParser();
var hostBase = Path.of("hosts/test/");
var config = new Configuration(Path.of("hosts/test/"));
db = new FileDB(Path.of("hosts/test/"), new DefaultEventBus(), (file) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@
*/
public class ContentParserNGTest {

CacheManager cacheManager = new CacheManager(new LocalCacheProvider());

@Test
public void testSomeMethod() throws IOException {
var contentParser = new DefaultContentParser(cacheManager);
var contentParser = new DefaultContentParser();

var expectedMD = """
Expand All @@ -61,7 +60,7 @@ public void testSomeMethod() throws IOException {

@Test
public void test_date() throws IOException {
var contentParser = new DefaultContentParser(cacheManager);
var contentParser = new DefaultContentParser();

var content = contentParser.parse(new NIOReadOnlyFile(Path.of("hosts/test/content/test.md"), Path.of("hosts/test/"))
);
Expand All @@ -80,7 +79,7 @@ public void test_date() throws IOException {

@Test
public void test_tags() throws IOException {
var contentParser = new DefaultContentParser(cacheManager);
var contentParser = new DefaultContentParser();

var content = contentParser.parse(new NIOReadOnlyFile(Path.of("hosts/test/content/tags.md"), Path.of("hosts/test/"))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ public class ContentRendererNGTest extends TemplateEngineTest {

static DefaultContentRenderer contentRenderer;
static MarkdownRenderer markdownRenderer;
static CacheManager cacheManager = new CacheManager(new LocalCacheProvider());

static ModuleManager moduleManager = new MockModuleManager();
static FileDB db;
static Path hostBase = Path.of("hosts/test/");

@BeforeAll
public static void beforeClass () throws IOException {
var contentParser = new DefaultContentParser(cacheManager);
var contentParser = new DefaultContentParser();
var config = new Configuration(Path.of("hosts/test/"));
db = new FileDB(Path.of("hosts/test/"), new DefaultEventBus(), (file) -> {
try {
Expand Down
Loading

0 comments on commit 0606875

Please sign in to comment.