Sling Model Caching | Community
Skip to main content
Level 2
January 21, 2022
Solved

Sling Model Caching

  • January 21, 2022
  • 1 reply
  • 2285 views

Hi All,

 

I'm trying to implement sling model caching in my project. I wanted to understand where the cached models are stored? Is there any specific dispatcher configuration required to implement sling model caching? Can we set a TTL for the cached models?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @h_malhotra 


What is the exact use case you have where you want to implement Sling model caching?

 

As per Sling documentation, By default, Sling Models do not do any caching of the adaptation result and every request for a model class will result in a new instance of the model class.

 

In general, it is strongly discouraged to store a reference to the original adaptable using the self injector. Using implementation version 1.4.8 or below, storing the original adaptable in a Sling Model, can cause heap space exhaustion, crashing the JVM. Starting in version 1.4.10, storing the original adaptable will not crash the JVM, but it can cause unexpected behavior (e.g. a model being created twice, when it should be cached). The issue was first reported in SLING-7586.

 

More details here:
https://sling.apache.org/documentation/bundles/models.html#caching

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sling-models-caching-use-case-amp-amp-how-to-invalidate-sling/td-p/357530

https://github.com/adobe/asset-share-commons/issues/177

 

Also here you do not have invalidate the cache as it's not cached in dispatcher.

 

Thanks!

1 reply

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
January 21, 2022

Hi @h_malhotra 


What is the exact use case you have where you want to implement Sling model caching?

 

As per Sling documentation, By default, Sling Models do not do any caching of the adaptation result and every request for a model class will result in a new instance of the model class.

 

In general, it is strongly discouraged to store a reference to the original adaptable using the self injector. Using implementation version 1.4.8 or below, storing the original adaptable in a Sling Model, can cause heap space exhaustion, crashing the JVM. Starting in version 1.4.10, storing the original adaptable will not crash the JVM, but it can cause unexpected behavior (e.g. a model being created twice, when it should be cached). The issue was first reported in SLING-7586.

 

More details here:
https://sling.apache.org/documentation/bundles/models.html#caching

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sling-models-caching-use-case-amp-amp-how-to-invalidate-sling/td-p/357530

https://github.com/adobe/asset-share-commons/issues/177

 

Also here you do not have invalidate the cache as it's not cached in dispatcher.

 

Thanks!