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
Solved! Go to Solution.
Views
Replies
Total Likes
Please use Sling Model caching with caution. As pointed in document
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.
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.
Please use Sling Model caching with caution. As pointed in document
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.
@AEMWizard 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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies