-
Notifications
You must be signed in to change notification settings - Fork 4.7k
chore: cache templates data #33439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
chore: cache templates data #33439
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0296eef
cache templates data
AnaghHegde c955b77
fix method parameters
AnaghHegde c240600
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde 53d1291
chore: add evict cache flow
AnaghHegde ad56029
evit cache flow
AnaghHegde c0ae652
review comments
AnaghHegde 0b6ce52
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde c4ecc4b
fix reference issue
AnaghHegde 68326a5
change data type to string
AnaghHegde 1da7601
add stop watch
AnaghHegde a911225
fix issue wioth component static method with Cache annotation
AnaghHegde 4d86f9f
update cache name
AnaghHegde afaedeb
add review comments and fix comments
AnaghHegde 6308d28
use in memory instead of release
AnaghHegde 00afdca
review comments
AnaghHegde 4cc1652
review comments
AnaghHegde 5c7f3d9
review comments
AnaghHegde ba35c3c
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde 1c7f7e3
fix cache logic
AnaghHegde d930c9d
add tests
AnaghHegde 3deb046
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde 2070521
reviw changes
AnaghHegde 86d38bd
reviw changes
AnaghHegde 799f449
update review comment
AnaghHegde 01a3c42
review comment
AnaghHegde 7b43c43
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
AnaghHegde 5cbcbd3
deep copy of POJOs to fix source not modified
AnaghHegde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
88 changes: 88 additions & 0 deletions
88
...er/appsmith-server/src/main/java/com/appsmith/server/helpers/CacheableTemplateHelper.java
This file contains hidden or 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,88 @@ | ||
| package com.appsmith.server.helpers; | ||
|
|
||
| import com.appsmith.caching.annotations.Cache; | ||
| import com.appsmith.external.converters.ISOStringToInstantConverter; | ||
| import com.appsmith.server.dtos.ApplicationJson; | ||
| import com.appsmith.server.dtos.ApplicationTemplate; | ||
| import com.appsmith.server.exceptions.AppsmithError; | ||
| import com.appsmith.server.exceptions.AppsmithException; | ||
| import com.appsmith.server.services.ce.ApplicationTemplateServiceCEImpl; | ||
| import com.appsmith.util.WebClientUtils; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.reactive.function.client.ExchangeStrategies; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
| import org.springframework.web.util.UriComponents; | ||
| import org.springframework.web.util.UriComponentsBuilder; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| @AllArgsConstructor | ||
| public class CacheableTemplateHelper { | ||
| @Cache(cacheName = "templateMetadata", key = "{#releaseVersion}") | ||
|
AnaghHegde marked this conversation as resolved.
Outdated
AnaghHegde marked this conversation as resolved.
Outdated
|
||
| public static Mono<List<ApplicationTemplate>> getTemplates(String releaseVersion, String baseUrl) { | ||
| UriComponentsBuilder uriComponentsBuilder = | ||
| UriComponentsBuilder.newInstance().queryParam("version", releaseVersion); | ||
|
|
||
| // uriComponents will build url in format: version=version&id=id1&id=id2&id=id3 | ||
| UriComponents uriComponents = uriComponentsBuilder.build(); | ||
|
|
||
| return WebClientUtils.create(baseUrl + "/api/v1/app-templates?" + uriComponents.getQuery()) | ||
| .get() | ||
| .exchangeToFlux(clientResponse -> { | ||
| if (clientResponse.statusCode().equals(HttpStatus.OK)) { | ||
| return clientResponse.bodyToFlux(ApplicationTemplate.class); | ||
| } else if (clientResponse.statusCode().isError()) { | ||
| return Flux.error( | ||
| new AppsmithException(AppsmithError.CLOUD_SERVICES_ERROR, clientResponse.statusCode())); | ||
|
AnaghHegde marked this conversation as resolved.
|
||
| } else { | ||
| return clientResponse.createException().flatMapMany(Flux::error); | ||
| } | ||
| }) | ||
| .collectList(); | ||
| } | ||
|
|
||
| @Cache(cacheName = "TemplateApplicationData", key = "{#templateId}") | ||
| public static Mono<ApplicationJson> getApplicationByTemplateId(String templateId, String baseUrl) { | ||
| final String templateUrl = baseUrl + "/api/v1/app-templates/" + templateId + "/application"; | ||
| /* | ||
| * using a custom url builder factory because default builder always encodes | ||
| * URL. | ||
| * It's expected that the appDataUrl is already encoded, so we don't need to | ||
| * encode that again. | ||
| * Encoding an encoded URL will not work and end up resulting a 404 error | ||
| */ | ||
| final int size = 4 * 1024 * 1024; // 4 MB | ||
| final ExchangeStrategies strategies = ExchangeStrategies.builder() | ||
| .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size)) | ||
| .build(); | ||
|
|
||
| WebClient webClient = WebClientUtils.builder() | ||
| .uriBuilderFactory(new ApplicationTemplateServiceCEImpl.NoEncodingUriBuilderFactory(templateUrl)) | ||
| .exchangeStrategies(strategies) | ||
| .build(); | ||
|
|
||
| return webClient | ||
| .get() | ||
| .retrieve() | ||
| .bodyToMono(String.class) | ||
| .map(jsonString -> { | ||
| Gson gson = new GsonBuilder() | ||
| .registerTypeAdapter(Instant.class, new ISOStringToInstantConverter()) | ||
| .create(); | ||
| Type fileType = new TypeToken<ApplicationJson>() {}.getType(); | ||
|
|
||
| ApplicationJson jsonFile = gson.fromJson(jsonString, fileType); | ||
| return jsonFile; | ||
| }) | ||
| .switchIfEmpty( | ||
| Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "template", templateId))); | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.