Expand my Community achievements bar.

SOLVED

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

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

 

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

View solution in original post

3 Replies

Avatar

Employee Advisor

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.

Avatar

Correct answer by
Community Advisor

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

 

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

Avatar

Administrator

@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