<mvc:resources> - 它是否缓存到内存中? Spring 4.0.5
<mvc:resources> - Does it cache to memory? Spring 4.0.5
让以下:
<mvc:resources mapping="/resources/**" location="/resources/" />
然后说
有 2000 个请求
/resources/script/app/myhax.js
如果我没有配置任何东西,myhax.js
是否以某种方式缓存到 RAM 中并从那里处理其余的请求,或者所有 2000 个请求都是从文件的真实路径(HDD,通常)? Spring 是否可以将此文件配置为在直接从内存中请求将来服务后将此文件保存在 RAM 中?
Spring 不缓存任何资源。但是有可能为了让资源被缓存。
您可以指定 cache-period(以给定的 max-age 值发送缓存 headers)例如
<resources mapping="/resources/**" location="/resources/" cache-period="3600"/>
或
mvc:resources
由 ResourceHttpRequestHandler 支持,因此您可以创建自己的子类来扩展 ResourceHttpRequestHandler
并通过覆盖适当的方法来实现缓存逻辑,例如 writeContent
(请注意可以参考文档或源代码以找出可用方法的列表)并在 spring config.
中使用这个新的子类
例如
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class CacheResourceHandler extends ResourceHttpRequestHandler {
private Map<URL, byte[]> cache = new HashMap<>();
@Override
protected void writeContent(HttpServletResponse resp, Resource rsc) throws IOException {
byte[] buff = cache.get(rsc.getURL());
//if not in cache
if (buff == null) {
//add to cache
buff = StreamUtils.copyToByteArray(rsc.getInputStream());
cache.put(rsc.getURL(), buff);
}
//return cache version
StreamUtils.copy(buff, resp.getOutputStream());
}
}
Xml配置
我们需要注释掉或删除之前的资源映射
<!--<resources mapping="/resources/**" location="/resources/" />-->
接下来我们需要声明我们的缓存处理程序 bean
<bean id="staticResources" class="CacheResourceHandler">
<property name="locations" value="/resources/"/>
</bean>
最后,我们将使用上面声明的实现 HandlerMapping
接口的 SimpleUrlHandlerMapping,以便从 URLS 映射到请求处理程序 bean。我们只需要传递我们的 bean 进行映射
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/resources/**=staticResources</value>
</property>
</bean>
让以下:
<mvc:resources mapping="/resources/**" location="/resources/" />
然后说
有 2000 个请求/resources/script/app/myhax.js
如果我没有配置任何东西,myhax.js
是否以某种方式缓存到 RAM 中并从那里处理其余的请求,或者所有 2000 个请求都是从文件的真实路径(HDD,通常)? Spring 是否可以将此文件配置为在直接从内存中请求将来服务后将此文件保存在 RAM 中?
Spring 不缓存任何资源。但是有可能为了让资源被缓存。
您可以指定 cache-period(以给定的 max-age 值发送缓存 headers)例如
<resources mapping="/resources/**" location="/resources/" cache-period="3600"/>
或
mvc:resources
由 ResourceHttpRequestHandler 支持,因此您可以创建自己的子类来扩展 ResourceHttpRequestHandler
并通过覆盖适当的方法来实现缓存逻辑,例如 writeContent
(请注意可以参考文档或源代码以找出可用方法的列表)并在 spring config.
中使用这个新的子类
例如
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class CacheResourceHandler extends ResourceHttpRequestHandler {
private Map<URL, byte[]> cache = new HashMap<>();
@Override
protected void writeContent(HttpServletResponse resp, Resource rsc) throws IOException {
byte[] buff = cache.get(rsc.getURL());
//if not in cache
if (buff == null) {
//add to cache
buff = StreamUtils.copyToByteArray(rsc.getInputStream());
cache.put(rsc.getURL(), buff);
}
//return cache version
StreamUtils.copy(buff, resp.getOutputStream());
}
}
Xml配置
我们需要注释掉或删除之前的资源映射
<!--<resources mapping="/resources/**" location="/resources/" />-->
接下来我们需要声明我们的缓存处理程序 bean
<bean id="staticResources" class="CacheResourceHandler">
<property name="locations" value="/resources/"/>
</bean>
最后,我们将使用上面声明的实现 HandlerMapping
接口的 SimpleUrlHandlerMapping,以便从 URLS 映射到请求处理程序 bean。我们只需要传递我们的 bean 进行映射
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/resources/**=staticResources</value>
</property>
</bean>