28

How do I prevent tomcat from caching? I have css and basic html files that I load and use via ajax, and unless I restart tomcat, the changes do not seem to reflect. Different machines, different browsers, and I don't receive the updated files.

Thoughts?

Stefan Kendall
  • 1,069
  • 3
  • 17
  • 33

4 Answers4

21

You might have to delete the application cache folder in /work/Catalina/localhost after changing the cachingAllowed flag.

Configuration can be introduced in server.xml as

<Context className="org.apache.catalina.core.StandardContext"
                 cachingAllowed="false"
                 charsetMapperClass="org.apache.catalina.util.CharsetMapper"
                 cookies="true" 
                 reloadable="false" 
                 wrapperClass="org.apache.catalina.core.StandardWrapper">
        </Context>
HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Adam Brand
  • 6,057
  • 2
  • 28
  • 40
17

For Tomcat 8 / Tomcat 9 properties should be added in conf/context.xml as follows

<Context>
  <Resources antiResourceLocking="false" cachingAllowed="false" />
  ...
</Context>

You might have to delete the application cache folder in /work/Catalina/localhost after changing the cachingAllowed flag. Also clear the cache of IntelliJ IDEA (if you use it to run Tomcat):

Mac:     /Users/{:user}/Library/Caches/IntelliJIdea{:version}/tomcat/  
Linux:   /home/{:user}/.IntelliJIdea{:version}/system/tomcat/
Windows: C:\Users\{:user}\.IntelliJIdea{:version}\system\tomcat\

See Apache Tomcat 9 Configuration Reference for other parameters.

  • 2
    antiResourceLocking on -Tag is definitively wrong for tomcat 8 – pscheit Apr 01 '16 at 12:13
  • This isn't correct for tomcat 7, either. "Setting property 'antiResourceLocking' to 'false' did not find a matching property." ... "Setting property 'cachingAllowed' to 'false' did not find a matching property." – Alkanshel Aug 23 '16 at 23:24
  • @Amalgovinus for Tomcat 7 see [Zalumon's answer](http://serverfault.com/a/451736/313654). – naXa stands with Ukraine Sep 22 '16 at 15:05
  • @Amalgovinus did you resolve your issue? I have same problem in tomcat7, what did you do? – Aaron Feb 27 '17 at 11:29
  • I never got the property to work.. but I'm pretty sure my cache was an unrelated deployment issue :( user error – Alkanshel Feb 27 '17 at 17:39
13

I had this problem in Tomcat 7 and the reason was that I had antiResourceLocking set to true (it sounded like a good idea...).

According to the docs ( http://tomcat.apache.org/tomcat-7.0-doc/config/context.html ):

Please note that setting this to true has some side effects, including the disabling of JSP reloading in a running server: see Bugzilla 37668.

In my case it even caused plain static text files to be cached.

So, in summary, at least for rapid development I had to use:

antiResourceLocking="false"
cachingAllowed="false"
Reto Höhener
  • 411
  • 3
  • 7
  • 15
4

Have you checked this documentation: Apache Tomcat Configuration Reference ?

cacheMaxSize -- Maximum size of the static resource cache in kilobytes. If not specified, the default value is 10240 (10 megabytes).

cacheTTL -- Amount of time in milliseconds between cache entries revalidation. If not specified, the default value is 5000 (5 seconds).

cachingAllowed -- If the value of this flag is true, the cache for static resources will be used. If not specified, the default value of the flag is true.

These parameters are the same for Tomcat 5.5 and Tomcat 6.0.

chicks
  • 3,639
  • 10
  • 26
  • 36
KPWINC
  • 11,274
  • 3
  • 36
  • 44