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

Avatar

Avatar
Boost 1
Level 2
AEMWizard
Level 2

Like

1 like

Total Posts

30 posts

Correct reply

1 solution
Top badges earned
Boost 1
Validate 10
Validate 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 1
Level 2
AEMWizard
Level 2

Like

1 like

Total Posts

30 posts

Correct reply

1 solution
Top badges earned
Boost 1
Validate 10
Validate 1
Applaud 5
Affirm 1
View profile
AEMWizard
Level 2

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
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,166 posts

Correct reply

1,080 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,166 posts

Correct reply

1,080 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
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.