Reputation: 87
I have a sling-model that needs to do some costly operations at initialization. These are related to the page scope, meaning based on currentPage
properties. Hence it would make sense that the model gets initialized only once per page call.
I found a nice article regarding this at how to call sling model only once where they are setting the cache
property of the Model
-annotation to true.
But this unfortunately only works per resource.
So setting the cache=true
property the model gets initialized for every component that calls it via data-sly-use
. It only helps wenn calling data-sly-use
in the same component (resource), but that's not helpful.
Can I somehow have the model cached for the entire lifecycle of the page-request?
Upvotes: 2
Views: 685
Reputation: 2322
SlingHttpServletRequest is a wrapper around HttpServletRequest and supports setAttribute and getAttribute methods provided by servlet api.
Setting attributes:
slingRequest.setAttribute(ATTRIBUTE_NAME, myobject);
Retrieve:
//check for null
MyClass myobject = (MyClass) slingRequest.getAttribute(ATTRIBUTE_NAME);
Upvotes: 1