简体   繁体   English

在Spring / Servlet / Tomcat中从内存提供静态资源的优雅方法?

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

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. 假设我想通过直接从RAM内存中加载.css.js和其他文件(图标,一些图像)的全部内容来提高性能,而不用打磁盘。

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? 我可以编写servlet / controller来将这些文件加载​​到上下文init或第一个请求中,并以简单的String缓存,但是是否有任何标准的解决方案可以在Spring,Servlets或Tomcat规范中为从RAM中选择的静态资源提供服务?

It would be nice to support proper headers also (cache-control etc.). 也支持适当的标头(缓存控制等)会很好。

Tomcat caches static files by default. Tomcat默认情况下会缓存静态文件。

You can configure caching behaviour in context.xml. 您可以在context.xml中配置缓存行为。 In particular (from http://tomcat.apache.org/tomcat-7.0-doc/config/context.html ): 特别是(来自http://tomcat.apache.org/tomcat-7.0-doc/config/context.html ):

cacheMaxSize cacheMaxSize
Maximum size of the static resource cache in kilobytes. 静态资源缓存的最大大小(以千字节为单位)。 If not specified, the default value is 10240 (10 megabytes). 如果未指定,则默认值为10240(10兆字节)。

cacheObjectMaxSize cacheObjectMaxSize
Maximum size of the static resource that will be placed in the cache. 将放置在缓存中的静态资源的最大大小。 If not specified, the default value is 512 (512 kilobytes). 如果未指定,则默认值为512(512 KB)。 If this value is greater than cacheMaxSize/20 it will be reduced to cacheMaxSize/20. 如果此值大于cacheMaxSize / 20,它将减小为cacheMaxSize / 20。

cacheTTL 缓存TTL
Amount of time in milliseconds between cache entries revalidation. 两次缓存条目重新验证之间的时间(以毫秒为单位)。 If not specified, the default value is 5000 (5 seconds). 如果未指定,则默认值为5000(5秒)。

cachingAllowed 允许缓存
If the value of this flag is true, the cache for static resources will be used. 如果此标志的值为true,则将使用静态资源的缓存。 If not specified, the default value of the flag is true. 如果未指定,则标志的默认值为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM