在Spring/Servlet/Tomcat中从内存提供静态资源的优雅方法? [英] Elegant way to serve static resources from memory in Spring / Servlet / Tomcat?

查看:46
本文介绍了在Spring/Servlet/Tomcat中从内存提供静态资源的优雅方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我可以编写servlet/控制器,将它们加载到上下文初始化或第一个请求中,并以简单的 String 进行缓存,但是已经有任何标准的解决方案可以服务于选定的静态资源是从Spring中的RAM,Servlet还是Tomcat规范?

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.).

推荐答案

Tomcat默认情况下会缓存静态文件.

Tomcat caches static files by default.

您可以在context.xml中配置缓存行为.特别是(摘自 http://tomcat.apache.org/tomcat-7.0-doc/config/context.html ):

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

cacheMaxSize
静态资源缓存的最大大小(以千字节为单位).如果未指定,则默认值为10240(10兆字节).

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

cacheObjectMaxSize
将放置在缓存中的静态资源的最大大小.如果未指定,则默认值为512(512 KB).如果此值大于cacheMaxSize/20,它将减小为cacheMaxSize/20.

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
两次缓存条目重新验证之间的时间(以毫秒为单位).如果未指定,则默认值为5000(5秒).

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

允许缓存
如果此标志的值为true,则将使用静态资源的缓存.如果未指定,则标志的默认值为true.

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.

这篇关于在Spring/Servlet/Tomcat中从内存提供静态资源的优雅方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆