-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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 all 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
13 changes: 13 additions & 0 deletions
13
...rver/appsmith-server/src/main/java/com/appsmith/server/dtos/CacheableApplicationJson.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,13 @@ | ||
| package com.appsmith.server.dtos; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| @Data | ||
| public class CacheableApplicationJson { | ||
|
|
||
| ApplicationJson applicationJson; | ||
|
|
||
| Instant cacheExpiryTime; | ||
| } |
14 changes: 14 additions & 0 deletions
14
.../appsmith-server/src/main/java/com/appsmith/server/dtos/CacheableApplicationTemplate.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,14 @@ | ||
| package com.appsmith.server.dtos; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| @Data | ||
| public class CacheableApplicationTemplate { | ||
|
|
||
| List<ApplicationTemplate> applicationTemplateList; | ||
|
|
||
| Instant cacheExpiryTime; | ||
| } |
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
147 changes: 147 additions & 0 deletions
147
...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,147 @@ | ||
| package com.appsmith.server.helpers; | ||
|
|
||
| import com.appsmith.external.converters.ISOStringToInstantConverter; | ||
| import com.appsmith.server.dtos.ApplicationJson; | ||
| import com.appsmith.server.dtos.ApplicationTemplate; | ||
| import com.appsmith.server.dtos.CacheableApplicationJson; | ||
| import com.appsmith.server.dtos.CacheableApplicationTemplate; | ||
| 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.extern.slf4j.Slf4j; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Component; | ||
| 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.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class CacheableTemplateHelper { | ||
| // Template metadata is used for showing the preview of the template | ||
|
|
||
| CacheableApplicationTemplate applicationTemplateList = new CacheableApplicationTemplate(); | ||
|
|
||
| Map<String, CacheableApplicationJson> cacheableApplicationJsonMap = new HashMap<>(); | ||
| private static final int CACHE_LIFE_TIME_IN_SECONDS = 60 * 60 * 24; // 24 hours | ||
|
|
||
| public Mono<CacheableApplicationTemplate> getTemplates(String releaseVersion, String baseUrl) { | ||
|
|
||
| if (applicationTemplateList == null) { | ||
| applicationTemplateList = new CacheableApplicationTemplate(); | ||
| } | ||
|
|
||
| if (applicationTemplateList.getCacheExpiryTime() != null | ||
| && isCacheValid(applicationTemplateList.getCacheExpiryTime())) { | ||
| return Mono.just(applicationTemplateList); | ||
| } | ||
|
|
||
| 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()) { | ||
| log.error("Error fetching templates from cloud services. Status code: {}", clientResponse); | ||
| return Flux.error( | ||
| new AppsmithException(AppsmithError.CLOUD_SERVICES_ERROR, clientResponse.statusCode())); | ||
| } else { | ||
| return clientResponse.createException().flatMapMany(Flux::error); | ||
| } | ||
| }) | ||
| .collectList() | ||
| .map(applicationTemplates -> { | ||
| applicationTemplateList.setApplicationTemplateList(applicationTemplates); | ||
| applicationTemplateList.setCacheExpiryTime(Instant.now()); | ||
| return applicationTemplateList; | ||
| }); | ||
| } | ||
|
|
||
| // Actual JSON object of the template | ||
| public Mono<CacheableApplicationJson> 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 | ||
|
|
||
| if (cacheableApplicationJsonMap == null) { | ||
| cacheableApplicationJsonMap = new HashMap<>(); | ||
| } | ||
|
|
||
| if (cacheableApplicationJsonMap.containsKey(templateId) | ||
| && isCacheValid(cacheableApplicationJsonMap.get(templateId).getCacheExpiryTime())) { | ||
| return Mono.just(getCacheableApplicationJsonCopy(cacheableApplicationJsonMap.get(templateId))); | ||
| } | ||
|
|
||
| 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 = getGson(); | ||
| Type fileType = new TypeToken<ApplicationJson>() {}.getType(); | ||
|
|
||
| CacheableApplicationJson cacheableApplicationJson = new CacheableApplicationJson(); | ||
| cacheableApplicationJson.setApplicationJson(gson.fromJson(jsonString, fileType)); | ||
| cacheableApplicationJson.setCacheExpiryTime(Instant.now()); | ||
|
|
||
| // Remove/replace the value from cache | ||
| cacheableApplicationJsonMap.put(templateId, cacheableApplicationJson); | ||
| return getCacheableApplicationJsonCopy(cacheableApplicationJson); | ||
| }) | ||
| .switchIfEmpty( | ||
| Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "template", templateId))); | ||
| } | ||
|
|
||
| private CacheableApplicationJson getCacheableApplicationJsonCopy(CacheableApplicationJson src) { | ||
| Gson gson = getGson(); | ||
| return gson.fromJson(gson.toJson(src), CacheableApplicationJson.class); | ||
| } | ||
|
|
||
| @NotNull private Gson getGson() { | ||
| return new GsonBuilder() | ||
| .registerTypeAdapter(Instant.class, new ISOStringToInstantConverter()) | ||
| .create(); | ||
| } | ||
|
|
||
| public boolean isCacheValid(Instant lastUpdatedAt) { | ||
| return Instant.now().minusSeconds(CACHE_LIFE_TIME_IN_SECONDS).isBefore(lastUpdatedAt); | ||
| } | ||
|
|
||
| public Map<String, CacheableApplicationJson> getCacheableApplicationJsonMap() { | ||
| return cacheableApplicationJsonMap; | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.