joerghoh commented on code in PR #26:
URL: 
https://github.com/apache/sling-org-apache-sling-scripting-sightly/pull/26#discussion_r1665878589


##########
src/main/java/org/apache/sling/scripting/sightly/impl/utils/ScriptDependencyResolver.java:
##########
@@ -74,60 +76,100 @@ public class ScriptDependencyResolver implements 
ResourceChangeListener, Externa
     private ResourceResolverFactory resourceResolverFactory;
 
     private Map<String, String> resolutionCache = new Cache(0);
-    private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
+    private final ReentrantReadWriteLock rwl = new 
ReentrantReadWriteLock(true);
     private final Lock readLock = rwl.readLock();
     private final Lock writeLock = rwl.writeLock();
 
+    private ServiceRegistration<ResourceChangeListener> 
resourceChangeListenerServiceRegistration;
+
+    private boolean cacheEnabled = false;
+
+    private static final String NOT_FOUND_MARKER = "#NOT_FOUND#";
+
+    @SuppressWarnings("squid:S1149")
     @Activate
     private void activate(ComponentContext componentContext) {
         int cacheSize = 
sightlyEngineConfiguration.getScriptResolutionCacheSize();
         if (cacheSize < 1024) {
+            cacheEnabled = false;
             resolutionCache = new Cache(0);
         } else {
+            cacheEnabled = true;
             resolutionCache = new Cache(cacheSize);
         }
-        componentContext.getBundleContext().addBundleListener(this);
+        if (cacheEnabled) {
+            componentContext.getBundleContext().addBundleListener(this);
+            Dictionary<String, Object> resourceChangeListenerProperties = new 
Hashtable<>();
+            resourceChangeListenerProperties.put(ResourceChangeListener.PATHS, 
".");
+            
resourceChangeListenerProperties.put(ResourceChangeListener.CHANGES, new 
String[] {
+                    ResourceChangeListener.CHANGE_ADDED,
+                    ResourceChangeListener.CHANGE_CHANGED,
+                    ResourceChangeListener.CHANGE_REMOVED
+            });
+            resourceChangeListenerServiceRegistration =
+                    
componentContext.getBundleContext().registerService(ResourceChangeListener.class,
 this, resourceChangeListenerProperties);
+        }
+    }
+
+    @Deactivate
+    private void deactivate(ComponentContext componentContext) {
+        if (resourceChangeListenerServiceRegistration != null) {
+            resourceChangeListenerServiceRegistration.unregister();
+        }
+        componentContext.getBundleContext().removeBundleListener(this);
     }
 
     public Resource resolveScript(RenderContext renderContext, String 
scriptIdentifier) {
+        SlingHttpServletRequest request = 
BindingsUtils.getRequest(renderContext.getBindings());
+        if (!cacheEnabled) {
+            return internalResolveScript(request, renderContext, 
scriptIdentifier);
+        }
         readLock.lock();
         try {
-            SlingHttpServletRequest request = 
BindingsUtils.getRequest(renderContext.getBindings());
             String cacheKey = request.getResource().getResourceType() + ":" + 
scriptIdentifier;
             Resource result = null;
             if (!resolutionCache.containsKey(cacheKey)) {
                 readLock.unlock();
                 writeLock.lock();
                 try {
-                    Resource caller =
-                            
ResourceResolution.getResourceForRequest(scriptingResourceResolverProvider.getRequestScopedResourceResolver(),
-                                    request);
-                    result = 
ResourceResolution.getResourceFromSearchPath(caller, scriptIdentifier);
-                    if (result == null) {
-                        SlingScriptHelper sling = 
BindingsUtils.getHelper(renderContext.getBindings());
-                        if (sling != null) {
-                            caller = 
getResource(scriptingResourceResolverProvider.getRequestScopedResourceResolver(),
-                                    sling.getScript().getScriptResource());
-                            result = 
ResourceResolution.getResourceFromSearchPath(caller, scriptIdentifier);
-                        }
-                    }
+                    result = internalResolveScript(request, renderContext, 
scriptIdentifier);

Review Comment:
   I am thinking of something like this:
   
   ```
   if (!resolutionCache.containsKey(cacheKey)) {
     readLock.unlock();
     result = internalResolveScript(request, renderContext, scriptIdentifier);
     writeLock.lock();
     try {
       if (result != null) {
         resolutionCache.put(cacheKey, result.getPath());
       } else {
         resolutionCache.put(cacheKey, NOT_FOUND_MARKER);
       }
       readLock.lock();
     } finally {
       writeLock.unlock();
     }
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to