gpt4 book ai didi

java - - Does it cache to memory? 春 4.0.5

转载 作者:行者123 更新时间:2023-11-30 08:57:59 25 4
gpt4 key购买 nike

让以下内容:

<mvc:resources mapping="/resources/**" location="/resources/" />

假设有 2000 个请求

/resources/script/app/myhax.js

如果我不配置任何东西,myhax.js 是否以某种方式缓存到 RAM 中并从那里处理其余的请求,或者所有 2000 个请求都是从文件的真实路径处理的(硬盘,通常)?是否可以将 Spring 配置为在直接从内存请求 future 服务后将此文件保存在 RAM 中?

最佳答案

Spring 不缓存任何资源。但是有可能为了让资源被缓存。

您可以指定 cache-period (发送具有给定最大年龄值的缓存 header )例如

<resources mapping="/resources/**" location="/resources/" cache-period="3600"/>

mvc:resourcesResourceHttpRequestHandler 支持,因此您可以创建自己的扩展 ResourceHttpRequestHandler 的子类,并通过覆盖适当的方法实现缓存逻辑,例如 writeContent(请注意,您可以引用文档或源代码以找出列表可用方法)并在 spring 配置中使用这个新的子类。

例如

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>

最后,我们将使用上面声明的 SimpleUrlHandlerMapping 实现 HandlerMapping 接口(interface),以便从 URLS 映射到请求处理程序 bean。我们所需要的只是传递我们的 bean 进行映射

   <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/resources/**=staticResources</value>
</property>
</bean>

关于java - <mvc :resources> - Does it cache to memory? 春 4.0.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27856218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com