0

Lets say I have a component A and component B. In component A, html file I am writing

   data-sly-use.head="com.Mymodel"

now in Component B also I need to use the same class, so in html file I have written

   data-sly-use.head="com.Mymodel"

Component A and Component B can be available in a page or may not be available at a time. What I want is that if both Component A and B present in a page then I do not want to initialise Sling model (com.Mymodel) twice. I would like to initialise only one time. How to do it?

Pakira
  • 1,951
  • 3
  • 25
  • 54
  • I'd wonder why? is this a pure performance concern? or is there a different reason? Don't get me wrong, you can use sling model caching as the answer blow suggests, I'm just wondering if your implementation requires one instance of the model for some other reason. – Ahmed Musallam Nov 09 '18 at 17:20

1 Answers1

3

Take a look at Sling Model Caching. This does exactly what you want, as long as it is adaptable from Resource.

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

You only have to specify cache=true in the Model-annotation.

@Model(adaptable = SlingHttpServletRequest.class, cache = true)
public class ModelClass {
   ...
}
Alexander Berndt
  • 1,628
  • 9
  • 17