[
https://issues.apache.org/jira/browse/SLING-11074?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17490116#comment-17490116
]
Dirk Rudolph commented on SLING-11074:
--------------------------------------
After some consideration this may not be the desired behaviour. For example,
consider an {{ImplementationPicker}} that selects a different implementation
based on the given adapter type. In this case a {{ModelA}} may be returned for
{{Type1}} and be cached for {{Type1}} and {{Type2}}. In a subsequent call the
picker may be supposed to return {{ModelB}} for {{Type2}} but as the instance
of {{ModelA}} is already cached for {{Type2}} it would be returned instead.
This may not be desired and become a breaking change for implementations that
rely on the current behaviour.
{code}
interface Type1 {}
interface Type2 {}
@Model class ModelA implements Type1, Type2 {}
@Model class ModelB implements Type1, Type2 {}
class CustomImplementationPicker implements ImplementationPicker { }
{code}
On the other hand it should be save to cache the model for the implementation
type returned by the implementation picker.
This will be done in SLING-11133 and this ticket be closed as "won't fix". If
we see need we could reopen it in the future.
> Cache model for all of its adapter types
> ----------------------------------------
>
> Key: SLING-11074
> URL: https://issues.apache.org/jira/browse/SLING-11074
> Project: Sling
> Issue Type: Improvement
> Components: Sling Models
> Reporter: Dirk Rudolph
> Assignee: Dirk Rudolph
> Priority: Major
> Time Spent: 20m
> Remaining Estimate: 0h
>
> Currently the ModelAdapterFactory caches a cacheable model for the requested
> type only
> https://github.com/apache/sling-org-apache-sling-models-impl/blob/master/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java#L430-L432
> {code}
> if (result.wasSuccessful() && modelAnnotation.cache() && adaptableCache !=
> null) {
> adaptableCache.put(requestedType, new
> SoftReference<Object>(result.getValue()));
> }
> {code}
> However, if a model is an adapter of multiple types an instance of it can
> also be returned for more than just the requested type.
> Caching the model for each of its adapter types may improve the cache hit
> ratio.
> {code}
> interface A {}
> interface B {}
> @Model(cache=true, adaptables=Resource.class, adapters={A.class, B.class})
> class Model implements A, B {}
> assertSame(givenResource.adaptTo(A.class), givenResource.adaptTo(B.class))
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)