Hello Team,
1: Caching sling model response:
From my sling model, I am hitting the 3rd party API. (API response will be same for, say 1 week). So, is it possible to cache the sling model response?
From this article(also from few other articles), caching from sling model is not the right way.
2: Caching sling servlet response: I heard, response from sling servlet is cacheable. If yes, I need to mention anything in sling servlet class, Dispatcher?
3: Caching the response from 3rd party API: What is the best way to cache the response from the 3rd party. I know that 3rd party API response is constant for 1 week. Even, if the API response changes for every day, I dont mind. Still will use the cached response for a week.
cc @Asutosh_Jena_ @SureshDhulipudi @aanchal-sikka @Jörg_Hoh
Solved! Go to Solution.
Views
Replies
Total Likes
In one of the Tuesday tech Bytes, @Anmol_Bhardwaj had recommended Sling Model caching for specific scenarios. If possible, please check with him once, he might be well aware of the issues and benefits.
Sling servlet response can be cached on dispatcher, but then the request needs to originate via SDI (SSI/ajax/ESI). If the cache expiration is TTL based, it would be refreshed periodically. Else, if we are dependent on .stat file validation, then we would need to invalidate it using Resource-based flush.
If you do not intend to make an HTTP call, you can write a scheduler+job which executes weekly to import the results into var nodes. The Sling Model can then access /var and render it.
Just a side note, if pages are cached on dispatcher. Hits on publish are reduced. Thus, reading /var from Sling Model should not impact performance.
Yes sling model responses can be cached kn dispatcher if you you implement sling model exporter in your sling model class. Actually, you can only cache the response on dispatcher which have file extension at the end of the response url. Check below to see how you can implement sling model exporter in aem https://experienceleague.adobe.com/en/docs/experience-manager-learn/foundation/development/develop-s...
normally, it’s not recommended to cache the 3rd party api response as it might change any time. In your use case even the 3rd party api response is same for a week, you don’t know at what time it will change in the future and you might have problem in identifying the exact time of the 3rd party response change and would have difficult time in doing those cache response flushed from the cache. So, it totally depends on you to decide how you can handle the flush of cached response in case it’s changed
1: Caching sling model response:
From my sling model, I am hitting the 3rd party API. (API response will be same for, say 1 week). So, is it possible to cache the sling model response?
as mentioned in the article caching from sling model is not the right way.
Sling Models are designed to use or mean to be Stateless - and scope should be limit to request (Request scoped) - caching within a Sling Model can lead to unexpected behavior.
2: Caching sling servlet response: I heard, response from sling servlet is cacheable. If yes, I need to mention anything in sling servlet class, Dispatcher?
Yes, This can be achieved.
In AEM we can cache the response from a Sling Servlet. This is typically done at the dispatcher level. You don't need to do anything special in the Sling Servlet itself, but you do need to configure your dispatcher to cache the response. This involves setting up a dispatcher caching rule that matches the URL pattern of your Sling Servlet.
ex: Sling Servlet is accessible at a URL like - /content/myapp/myservlet
/cache
{
/rules
{
/0000
{
/glob "* /content/myapp/myservlet*"
/type "allow"
}
3: Caching the response from 3rd party API: What is the best way to cache the response from the 3rd party. I know that 3rd party API response is constant for 1 week. Even, if the API response changes for every day, I dont mind. Still will use the cached response for a week.
one common approach is to use a caching HTTP client. This can be configured to cache responses based on their HTTP headers, or you can implement custom caching rules.
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(8192)
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30000)
.setSocketTimeout(30000)
.build();
CloseableHttpClient cachingClient = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setDefaultRequestConfig(requestConfig)
.build();
use this - CloseableHttpClient to make API calls or requests and apply the responses will be cached as per the configuration.
In one of the Tuesday tech Bytes, @Anmol_Bhardwaj had recommended Sling Model caching for specific scenarios. If possible, please check with him once, he might be well aware of the issues and benefits.
Sling servlet response can be cached on dispatcher, but then the request needs to originate via SDI (SSI/ajax/ESI). If the cache expiration is TTL based, it would be refreshed periodically. Else, if we are dependent on .stat file validation, then we would need to invalidate it using Resource-based flush.
If you do not intend to make an HTTP call, you can write a scheduler+job which executes weekly to import the results into var nodes. The Sling Model can then access /var and render it.
Just a side note, if pages are cached on dispatcher. Hits on publish are reduced. Thus, reading /var from Sling Model should not impact performance.
Thanks @aanchal-sikka for the response.
As you have mentioned: "If you do not intend to make an HTTP call, you can write a scheduler+job which executes weekly to import the results into var nodes." this is good one. As a developer, we should think in different aspects as well.
I was reviewing some sling model code. At that time, I came across these snippet.
Since, There is no concept of caching in sling model, then this line: response.setHeader("Dispatcher", "no-cache"); is required in init method? I think, this is not required.
yes @Mahesh_Gunaje , it can be removed.
Views
Likes
Replies
Views
Likes
Replies