Sling Models Caching Use Case && How to invalidate Sling Model Cache? | Community
Skip to main content
Level 6
March 31, 2020
Solved

Sling Models Caching Use Case && How to invalidate Sling Model Cache?

  • March 31, 2020
  • 3 replies
  • 3575 views

Hello Community, 

A. Can anyone please shed some knowledge of different ways that we can utilise the Sling Models caching feature?

B. Please explain the risks of using the Sling Models caching feature?
C. Please explain how we would invalidate Sling Model Cache?

Example Code:

@Model(adaptable = SlingHttpServletRequest.class, cache = true) public class ModelClass {} ... // assume that request is some SlingHttpServletRequest object ModelClass object1 = request.adaptTo(ModelClass.class); // creates new instance of ModelClass ModelClass object2 = modelFactory.createModel(request, ModelClass.class); // Sling Models returns the cached instance assert object1 == object2;

Documentation: https://sling.apache.org/documentation/bundles/models.html#caching

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 aanchal-sikka

Please use Sling Model caching with caution. As pointed in document 

https://sling.apache.org/documentation/bundles/models.html#a-note-about-cache--true-and-using-the-self-injector 

 

A note about cache = true and using the self injector
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.

The problem can be avoided by discarding the original adaptable when it is no longer needed.

3 replies

joerghoh
Adobe Employee
Adobe Employee
April 3, 2020

What detail exactly isn't covered by the documentation you referenced? IIRC caching is enabled by default, and there should never be a need to manually invalidate that cache.

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
October 31, 2023

Please use Sling Model caching with caution. As pointed in document 

https://sling.apache.org/documentation/bundles/models.html#a-note-about-cache--true-and-using-the-self-injector 

 

A note about cache = true and using the self injector
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.

The problem can be avoided by discarding the original adaptable when it is no longer needed.

Aanchal Sikka
kautuk_sahni
Community Manager
Community Manager
November 6, 2023

@supportmember Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni