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

Avatar

Avatar

AEMWizard

Avatar

AEMWizard

AEMWizard

31-03-2020

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

Accepted Solutions (0)

Answers (1)

Answers (1)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.1K

Likes

1.1K

Correct Reply

1.1K

Avatar

Jörg_Hoh

Employee

Total Posts

3.1K

Likes

1.1K

Correct Reply

1.1K
Jörg_Hoh
Employee

03-04-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.