嗯,你说你想要cache
你的底层目标资源的全部内容,你必须从inputStream
缓存它的byte[]
。
由于<mvc:resources>
得到ResourceHttpRequestHandler
的支持,所以没有停止来编写自己的子类并直接使用它来代替自定义标记。
而仅有不到overrided writeContent
方法实现您的高速缓存逻辑:
public class CacheableResourceHttpRequestHandler extends ResourceHttpRequestHandler {
private Map<URL, byte[]> cache = new HashMap<URL, byte[]>();
@Override
protected void writeContent(HttpServletResponse response, Resource resource) throws IOException {
byte[] content = this.cache.get(resource.getURL());
if (content == null) {
content = StreamUtils.copyToByteArray(resource.getInputStream());
this.cache.put(resource.getURL(), content);
}
StreamUtils.copy(content, response.getOutputStream());
}
}
而且使用它从Spring配置为通用豆:
<bean id="staticResources" class="com.my.proj.web.CacheableResourceHttpRequestHandler">
<property name="locations" value="/public-resources/"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/resources/**=staticResources</value>
</property>
</bean>
不'缓存period'回答你的问题? –
@ArtemBilan据我所知,它涉及进一步的客户端请求和HTTP响应(未修改),但是如果新客户端需要下载文件(访问页面),则会发生磁盘读取。 –