Elegant way to serve static resources from memory in Spring / Servlet / Tomcat?

落花浮王杯 提交于 2020-01-25 10:31:26

问题


Suppose I want to improve performance by loading whole contents of .css, .js and some other files (favicon, some images) directly from RAM memory without hitting disk.

I can just write servlet/controller which will load those files on context init or first request and cache in simple String, but is there any standard already made solution to serve selected static resources from RAM in Spring, Servlets or Tomcat specification?

It would be nice to support proper headers also (cache-control etc.).


回答1:


Tomcat caches static files by default.

You can configure caching behaviour in context.xml. In particular (from http://tomcat.apache.org/tomcat-7.0-doc/config/context.html):

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

cacheObjectMaxSize
Maximum size of the static resource that will be placed in the cache. If not specified, the default value is 512 (512 kilobytes). If this value is greater than cacheMaxSize/20 it will be reduced to cacheMaxSize/20.

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.



来源:https://stackoverflow.com/questions/22778293/elegant-way-to-serve-static-resources-from-memory-in-spring-servlet-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!