-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: add caching for extension getter to enhance performance
- Loading branch information
Showing
7 changed files
with
155 additions
and
53 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
application/src/main/java/run/halo/app/plugin/extensionpoint/AbstractDefinitionGetter.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,44 @@ | ||
package run.halo.app.plugin.extensionpoint; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.DisposableBean; | ||
import run.halo.app.extension.Extension; | ||
import run.halo.app.extension.ExtensionClient; | ||
import run.halo.app.extension.controller.Controller; | ||
import run.halo.app.extension.controller.ControllerBuilder; | ||
import run.halo.app.extension.controller.Reconciler; | ||
|
||
@RequiredArgsConstructor | ||
abstract class AbstractDefinitionGetter<E extends Extension> | ||
implements Reconciler<Reconciler.Request>, DisposableBean { | ||
|
||
protected final ConcurrentMap<String, E> cache = new ConcurrentHashMap<>(); | ||
|
||
private final ExtensionClient client; | ||
|
||
private final E watchType; | ||
|
||
abstract void putCache(E definition); | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Result reconcile(Request request) { | ||
client.fetch((Class<E>) watchType.getClass(), request.name()) | ||
.ifPresent(this::putCache); | ||
return Result.doNotRetry(); | ||
} | ||
|
||
@Override | ||
public Controller setupWith(ControllerBuilder builder) { | ||
return builder.extension(watchType) | ||
.syncAllOnStart(true) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void destroy() throws Exception { | ||
cache.clear(); | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
application/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionDefinitionGetter.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,13 @@ | ||
package run.halo.app.plugin.extensionpoint; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public interface ExtensionDefinitionGetter { | ||
|
||
/** | ||
* Gets extension definition by extension definition name. | ||
* | ||
* @param name extension definition name | ||
*/ | ||
Mono<ExtensionDefinition> get(String name); | ||
} |
25 changes: 25 additions & 0 deletions
25
...ation/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionDefinitionGetterImpl.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,25 @@ | ||
package run.halo.app.plugin.extensionpoint; | ||
|
||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.extension.ExtensionClient; | ||
|
||
@Component | ||
public class ExtensionDefinitionGetterImpl | ||
extends AbstractDefinitionGetter<ExtensionDefinition> | ||
implements ExtensionDefinitionGetter { | ||
|
||
public ExtensionDefinitionGetterImpl(ExtensionClient client) { | ||
super(client, new ExtensionDefinition()); | ||
} | ||
|
||
@Override | ||
public Mono<ExtensionDefinition> get(String name) { | ||
return Mono.fromSupplier(() -> cache.get(name)); | ||
} | ||
|
||
@Override | ||
void putCache(ExtensionDefinition definition) { | ||
cache.put(definition.getMetadata().getName(), definition); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...tion/src/main/java/run/halo/app/plugin/extensionpoint/ExtensionPointDefinitionGetter.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,14 @@ | ||
package run.halo.app.plugin.extensionpoint; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public interface ExtensionPointDefinitionGetter { | ||
|
||
/** | ||
* Gets extension point definition by extension point class. | ||
* <p>Retrieve by filedSelector: <code>spec.className</code></p> | ||
* | ||
* @param className extension point class name | ||
*/ | ||
Mono<ExtensionPointDefinition> getByClassName(String className); | ||
} |
26 changes: 26 additions & 0 deletions
26
.../src/main/java/run/halo/app/plugin/extensionpoint/ExtensionPointDefinitionGetterImpl.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,26 @@ | ||
package run.halo.app.plugin.extensionpoint; | ||
|
||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.extension.ExtensionClient; | ||
|
||
@Component | ||
public class ExtensionPointDefinitionGetterImpl | ||
extends AbstractDefinitionGetter<ExtensionPointDefinition> | ||
implements ExtensionPointDefinitionGetter { | ||
|
||
public ExtensionPointDefinitionGetterImpl(ExtensionClient client) { | ||
super(client, new ExtensionPointDefinition()); | ||
} | ||
|
||
@Override | ||
public Mono<ExtensionPointDefinition> getByClassName(String className) { | ||
return Mono.fromSupplier(() -> cache.get(className)); | ||
} | ||
|
||
@Override | ||
void putCache(ExtensionPointDefinition definition) { | ||
var className = definition.getSpec().getClassName(); | ||
cache.put(className, definition); | ||
} | ||
} |
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