Expand my Community achievements bar.

SOLVED

Sling Model Caching

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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-c...

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!

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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-c...

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!