This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push: new 9cf432c Provide a little more control over resource caching 9cf432c is described below commit 9cf432cecde096f10ea19ff78d49b03307f85099 Author: Mark Thomas <ma...@apache.org> AuthorDate: Thu Nov 4 10:52:06 2021 +0000 Provide a little more control over resource caching --- java/org/apache/catalina/WebResourceRoot.java | 44 ++++++++++++++++++++++ .../apache/catalina/startup/ContextRuleSet.java | 8 ++++ java/org/apache/catalina/webresources/Cache.java | 17 +++++++++ .../catalina/webresources/MyCacheStrategy.java | 29 ++++++++++++++ .../apache/catalina/webresources/StandardRoot.java | 11 ++++++ webapps/docs/changelog.xml | 5 +++ 6 files changed, 114 insertions(+) diff --git a/java/org/apache/catalina/WebResourceRoot.java b/java/org/apache/catalina/WebResourceRoot.java index 8d8092a..ae37893 100644 --- a/java/org/apache/catalina/WebResourceRoot.java +++ b/java/org/apache/catalina/WebResourceRoot.java @@ -431,10 +431,54 @@ public interface WebResourceRoot extends Lifecycle { */ void gc(); + /** + * Obtain the current caching strategy. + * <p> + * The default implementation returns {@code null}. Sub-classes wishing to + * utilise a {@link CacheStrategy} should provide an appropriate + * implementation. + * + * @return the current caching strategy or {@code null} if no strategy has + * been configured + */ + default CacheStrategy getCacheStrategy() { + return null; + } + + /** + * Set the current caching strategy. + * <p> + * The default implementation is a NO-OP. Sub-classes wishing to utilise a + * {@link CacheStrategy} should provide an appropriate implementation. + * + * @param strategy The new strategy to use or {@code null} for no strategy + */ + default void setCacheStrategy(CacheStrategy strategy) { + // NO-OP + } + enum ResourceSetType { PRE, RESOURCE_JAR, POST, CLASSES_JAR } + + /** + * Provides a mechanism to modify the caching behaviour. + */ + interface CacheStrategy { + + /** + * Should the result of looking up the resource at the given path be + * excluded from caching? + * + * @param path The path to check against the strategy to see if the + * result should be cached + * + * @return {@code true} if the result should not be cached, otherwise + * {@code false} + */ + boolean noCache(String path); + } } diff --git a/java/org/apache/catalina/startup/ContextRuleSet.java b/java/org/apache/catalina/startup/ContextRuleSet.java index e230d54..4aa081e 100644 --- a/java/org/apache/catalina/startup/ContextRuleSet.java +++ b/java/org/apache/catalina/startup/ContextRuleSet.java @@ -168,6 +168,14 @@ public class ContextRuleSet implements RuleSet { "setResources", "org.apache.catalina.WebResourceRoot"); + digester.addObjectCreate(prefix + "Context/Resources/CacheStrategy", + null, // MUST be specified in the element + "className"); + digester.addSetProperties(prefix + "Context/Resources/CacheStrategy"); + digester.addSetNext(prefix + "Context/Resources/CacheStrategy", + "setCacheStrategy", + "org.apache.catalina.WebResourceRoot$CacheStrategy"); + digester.addObjectCreate(prefix + "Context/Resources/PreResources", null, // MUST be specified in the element "className"); diff --git a/java/org/apache/catalina/webresources/Cache.java b/java/org/apache/catalina/webresources/Cache.java index b2ce23c..bf762d4 100644 --- a/java/org/apache/catalina/webresources/Cache.java +++ b/java/org/apache/catalina/webresources/Cache.java @@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; import org.apache.catalina.WebResource; +import org.apache.catalina.WebResourceRoot.CacheStrategy; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.res.StringManager; @@ -45,6 +46,7 @@ public class Cache { private long ttl = 5000; private long maxSize = 10 * 1024 * 1024; private int objectMaxSize = (int) maxSize/OBJECT_MAX_SIZE_FACTOR; + private CacheStrategy cacheStrategy; private AtomicLong lookupCount = new AtomicLong(0); private AtomicLong hitCount = new AtomicLong(0); @@ -62,6 +64,13 @@ public class Cache { return root.getResourceInternal(path, useClassLoaderResources); } + CacheStrategy strategy = getCacheStrategy(); + if (strategy != null) { + if (strategy.noCache(path)) { + return root.getResourceInternal(path, useClassLoaderResources); + } + } + lookupCount.incrementAndGet(); CachedResource cacheEntry = resourceCache.get(path); @@ -261,6 +270,14 @@ public class Cache { } } + public CacheStrategy getCacheStrategy() { + return cacheStrategy; + } + + public void setCacheStrategy(CacheStrategy cacheStrategy) { + this.cacheStrategy = cacheStrategy; + } + public long getTtl() { return ttl; } diff --git a/java/org/apache/catalina/webresources/MyCacheStrategy.java b/java/org/apache/catalina/webresources/MyCacheStrategy.java new file mode 100644 index 0000000..ac23782 --- /dev/null +++ b/java/org/apache/catalina/webresources/MyCacheStrategy.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.webresources; + +import org.apache.catalina.WebResourceRoot.CacheStrategy; + +public class MyCacheStrategy implements CacheStrategy { + + + @Override + public boolean noCache(String path) { + System.out.println(path); + return true; + } +} diff --git a/java/org/apache/catalina/webresources/StandardRoot.java b/java/org/apache/catalina/webresources/StandardRoot.java index 6238e39..ec28485 100644 --- a/java/org/apache/catalina/webresources/StandardRoot.java +++ b/java/org/apache/catalina/webresources/StandardRoot.java @@ -498,6 +498,17 @@ public class StandardRoot extends LifecycleMBeanBase implements WebResourceRoot return cachingAllowed; } + + @Override + public CacheStrategy getCacheStrategy() { + return cache.getCacheStrategy(); + } + + @Override + public void setCacheStrategy(CacheStrategy strategy) { + cache.setCacheStrategy(strategy); + } + @Override public long getCacheTtl() { return cache.getTtl(); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 6012049..74cd374 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -116,6 +116,11 @@ Add Javadoc comment which listeners must be nested whithin <code>Server</code> elements only. (michaelo) </docs> + <add> + Add support for custom caching strategies for web application resources. + This initial implementation allows control over whether or not a + resource is cached. (markt) + </add> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org