Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-11133

Cache model for its implementation type

    XMLWordPrintableJSON

Details

    Description

      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

      if (result.wasSuccessful() && modelAnnotation.cache() && adaptableCache != null) {
        adaptableCache.put(requestedType, new SoftReference<Object>(result.getValue()));
      }
      

      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.

      interface AdapterType1 {}
      interface AdapterType2 {}
      
      @Model(adaptables = Resource.class, adapterType={AdapterType1.class, AdapterType2.class}, cache = true) 
      class Model implements AdapterType1, AdapterType2 {}
      
      assertSame(resource.adaptTo(Model.class), resouce.adaptTo(AdapterType1.class));
      assertSame(resource.adaptTo(AdapterType1.class), resouce.adaptTo(AdapterType2.class));
      

      While it is not save to cache the model for all of its adapter types (see SLING-11074), it is certainly save to cache the Model for its implementation type.

      In fact, when caching the Model for the implementation type, caching it for the requested type is redundant. This is based on the assumption that the org.apache.sling.models.impl.AdapterImplementations#lookup() always returns the same implementation for given adapterType and adaptable.

      Attachments

        Activity

          People

            diru Dirk Rudolph
            diru Dirk Rudolph
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 40m
                1h 40m