Skip to content

Commit 67721db

Browse files
authored
Merge pull request #213 from RWS/Improve-StaticContentItems-Caching
Adding a cache wrapper around the call to getting StaticContentItems.
2 parents 6845d14 + c303c10 commit 67721db

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

dxa-framework/dxa-tridion-provider/src/main/java/com/sdl/dxa/tridion/mapping/impl/GraphQLContentProvider.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sdl.dxa.api.datamodel.model.ContentModelData;
44
import com.sdl.dxa.api.datamodel.model.EntityModelData;
55
import com.sdl.dxa.api.datamodel.model.PageModelData;
6+
import com.sdl.dxa.caching.NamedCacheProvider;
67
import com.sdl.dxa.caching.statistics.CacheStatisticsProvider;
78
import com.sdl.dxa.common.dto.EntityRequestDto;
89
import com.sdl.dxa.common.dto.PageRequestDto;
@@ -44,10 +45,13 @@
4445
import org.springframework.web.context.request.RequestContextHolder;
4546

4647
import jakarta.servlet.http.HttpSession;
48+
49+
import javax.cache.Cache;
4750
import java.io.Serializable;
4851
import java.util.HashMap;
4952
import java.util.List;
5053
import java.util.Map;
54+
import java.util.UUID;
5155
import java.util.stream.Collectors;
5256

5357
/**
@@ -65,6 +69,7 @@ public class GraphQLContentProvider extends AbstractContentProvider implements C
6569
private final WebRequestContext webRequestContext;
6670
private ApiClientProvider pcaClientProvider;
6771
private CacheManager cacheManager;
72+
private NamedCacheProvider namedCacheProvider;
6873
private CacheStatisticsProvider cacheStatisticsProvider;
6974

7075
@Autowired
@@ -73,6 +78,7 @@ public GraphQLContentProvider(WebRequestContext webRequestContext,
7378
ModelBuilderPipeline builderPipeline, GraphQLProvider graphQLProvider,
7479
ApiClientProvider pcaClientProvider,
7580
@Qualifier("compositeCacheManager") CacheManager cacheManager,
81+
NamedCacheProvider namedCacheProvider,
7682
CacheStatisticsProvider cacheStatisticsProvider) {
7783
super(webRequestContext, cacheManager);
7884
this.webRequestContext = webRequestContext;
@@ -81,6 +87,7 @@ public GraphQLContentProvider(WebRequestContext webRequestContext,
8187
this.staticContentResolver = staticContentResolver;
8288
this.builderPipeline = builderPipeline;
8389
this.graphQLProvider = graphQLProvider;
90+
this.namedCacheProvider = namedCacheProvider;
8491
this.cacheStatisticsProvider = cacheStatisticsProvider;
8592
}
8693

@@ -160,23 +167,36 @@ private boolean isNoMediaCache(String path, String localizationPath) {
160167
return !FileUtils.isEssentialConfiguration(path, localizationPath) && webRequestContext.isSessionPreview();
161168
}
162169

163-
/**
164-
* {@inheritDoc}
165-
*
166-
*/
167170
@Override
168171
public @NotNull StaticContentItem getStaticContent(String path, String localizationId, String localizationPath)
169172
throws ContentProviderException {
173+
if (namedCacheProvider == null || !namedCacheProvider.isCacheEnabled("staticContentItems")) {
174+
return processStaticContent(path, localizationId, localizationPath);
175+
}
176+
else {
177+
String cacheKeyInput = String.format("%s%s%s", path, localizationId, localizationPath);
178+
String cacheKey = UUID.nameUUIDFromBytes(cacheKeyInput.getBytes()).toString();
179+
Cache<Object, Object> staticContentItemsCache = namedCacheProvider.getCache("staticContentItems");
180+
StaticContentItem staticContentItem = (StaticContentItem)staticContentItemsCache.get(cacheKey);
181+
if (staticContentItem == null) {
182+
staticContentItem = processStaticContent(path, localizationId, localizationPath);
183+
staticContentItemsCache.put(cacheKey, staticContentItem);
184+
if (this.cacheStatisticsProvider != null) {
185+
this.cacheStatisticsProvider.storeStatsInfo("staticContentItems", staticContentItem);
186+
}
187+
}
188+
return staticContentItem;
189+
}
190+
}
191+
192+
public @NotNull StaticContentItem processStaticContent(String path, String localizationId, String localizationPath)
193+
throws ContentProviderException {
170194
StaticContentRequestDto requestDto = StaticContentRequestDto.builder(path, localizationId)
171195
.localizationPath(localizationPath)
172196
.baseUrl(webRequestContext.getBaseUrl())
173197
.noMediaCache(isNoMediaCache(path, localizationPath))
174198
.build();
175-
StaticContentItem staticContentItem = staticContentResolver.getStaticContent(requestDto);
176-
if (cacheStatisticsProvider != null) {
177-
cacheStatisticsProvider.storeStatsInfo("staticContentItems", staticContentItem);
178-
}
179-
return staticContentItem;
199+
return staticContentResolver.getStaticContent(requestDto);
180200
}
181201

182202
protected PageModel loadPage(String path, Localization localization) throws ContentProviderException {

dxa-framework/dxa-tridion-provider/src/test/java/com/sdl/dxa/tridion/mapping/impl/GraphQLContentProviderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void setup() {
6767
graphQLProvider,
6868
apiClientProvider,
6969
cacheManager,
70+
null,
7071
cacheStatisticsProvider
7172
));
7273
}
@@ -103,7 +104,7 @@ public void getStaticContent() throws Exception {
103104
when(staticContentResolver.getStaticContent(any(StaticContentRequestDto.class))).thenReturn(new StaticContentItem("testType",
104105
contentFile, false));
105106

106-
StaticContentItem result = contentProvider.getStaticContent("/static", "localizationId", "localilzationPath");
107+
StaticContentItem result = contentProvider.getStaticContent("/static", "localizationId", "localizationPath");
107108

108109
assertEquals("path", contentFile.getName());
109110
assertEquals("testType", result.getContentType());

dxa-webapp/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.sdl.dxa</groupId>
77
<artifactId>dxa-oss-parent</artifactId>
8-
<version>2.3.5-SNAPSHOT</version>
8+
<version>2.3.7-SNAPSHOT</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

@@ -201,7 +201,7 @@
201201
<dependency>
202202
<groupId>com.sdl.dxa</groupId>
203203
<artifactId>dxa-oss-parent</artifactId>
204-
<version>2.3.5-SNAPSHOT</version>
204+
<version>2.3.7-SNAPSHOT</version>
205205
<type>pom</type>
206206
<scope>import</scope>
207207
</dependency>

0 commit comments

Comments
 (0)