Elegant way to serve static resources from memory

2019-09-07 10: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条回答
叼着烟拽天下
2楼-- · 2019-09-07 11:16

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.

查看更多
登录 后发表回答