This commit is contained in:
Redkale
2016-08-08 10:48:46 +08:00
parent bf9098df86
commit 42e7ac298e
4 changed files with 22 additions and 6 deletions

View File

@@ -163,8 +163,9 @@
资源缓存的配置, 默认存在一个含默认属性的caches节点 资源缓存的配置, 默认存在一个含默认属性的caches节点
limit: 资源缓存最大容量, 默认: 0, 为0表示不缓存 单位可以是B、K、M、G不区分大小写 limit: 资源缓存最大容量, 默认: 0, 为0表示不缓存 单位可以是B、K、M、G不区分大小写
lengthmax: 可缓存的文件大小上限, 默认: 1M超过1M的文件不会被缓存 lengthmax: 可缓存的文件大小上限, 默认: 1M超过1M的文件不会被缓存
watch: 是否监控缓存文件的变化, 默认不监控
--> -->
<caches limit="0M" lengthmax="1M" /> <cache limit="0M" lengthmax="1M" watch="false"/>
<!-- <!--
支持类似nginx中的rewrite 目前只支持静态资源对静态资源的跳转。 支持类似nginx中的rewrite 目前只支持静态资源对静态资源的跳转。
type: 匹配的类型, 目前只支持location(匹配requestURI), 默认: location type: 匹配的类型, 目前只支持location(匹配requestURI), 默认: location

View File

@@ -145,6 +145,10 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
return this.logger; return this.logger;
} }
public PrepareServlet<K, C, R, P, S> getPrepareServlet(){
return this.prepare;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) { public void addServlet(S servlet, final Object attachment, AnyValue conf, K... mappings) {
this.prepare.addServlet(servlet, attachment, conf, mappings); this.prepare.addServlet(servlet, attachment, conf, mappings);

View File

@@ -56,9 +56,11 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
((DefaultAnyValue) resConfig).addValue("webroot", config.getValue("root")); ((DefaultAnyValue) resConfig).addValue("webroot", config.getValue("root"));
} }
} }
if (resConfig == null) resConfig = config.getAnyValue("resource-servlet"); //兼容
if (resConfig == null) { if (resConfig == null) {
DefaultAnyValue dresConfig = new DefaultAnyValue(); DefaultAnyValue dresConfig = new DefaultAnyValue();
dresConfig.addValue("webroot", config.getValue("root")); dresConfig.addValue("webroot", config.getValue("root"));
dresConfig.addValue("cache", config.getAnyValue("cache"));
resConfig = dresConfig; resConfig = dresConfig;
} }
this.resourceHttpServlet.init(context, resConfig); this.resourceHttpServlet.init(context, resConfig);
@@ -142,6 +144,10 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
} }
} }
public HttpServlet getResourceServlet() {
return this.resourceHttpServlet;
}
@Override @Override
public void destroy(HttpContext context, AnyValue config) { public void destroy(HttpContext context, AnyValue config) {
this.resourceHttpServlet.destroy(context, config); this.resourceHttpServlet.destroy(context, config);

View File

@@ -80,14 +80,17 @@ public final class HttpResourceServlet extends HttpServlet {
protected final boolean finest = logger.isLoggable(Level.FINEST); protected final boolean finest = logger.isLoggable(Level.FINEST);
protected final LongAdder cachedLength = new LongAdder();
//缓存总大小, 默认0 //缓存总大小, 默认0
protected long cachelimit = 0 * 1024 * 1024L; protected long cachelimit = 0 * 1024 * 1024L;
protected final LongAdder cachedLength = new LongAdder();
//最大可缓存的文件大小, 大于该值的文件将不被缓存 //最大可缓存的文件大小, 大于该值的文件将不被缓存
protected long cachelengthmax = 1 * 1024 * 1024; protected long cachelengthmax = 1 * 1024 * 1024;
//是否监控缓存文件的变化, 默认不监控
protected boolean watch = false;
protected File root = new File("./root/"); protected File root = new File("./root/");
protected String indexHtml = "index.html"; protected String indexHtml = "index.html";
@@ -117,10 +120,12 @@ public final class HttpResourceServlet extends HttpServlet {
} catch (IOException ioe) { } catch (IOException ioe) {
this.root = new File(rootstr); this.root = new File(rootstr);
} }
AnyValue cacheconf = config.getAnyValue("caches"); AnyValue cacheconf = config.getAnyValue("cache");
if (cacheconf == null) cacheconf = config.getAnyValue("caches"); //兼容旧参数
if (cacheconf != null) { if (cacheconf != null) {
this.cachelimit = parseLenth(cacheconf.getValue("limit"), 0 * 1024 * 1024L); this.cachelimit = parseLenth(cacheconf.getValue("limit"), 0 * 1024 * 1024L);
this.cachelengthmax = parseLenth(cacheconf.getValue("lengthmax"), 1 * 1024 * 1024L); this.cachelengthmax = parseLenth(cacheconf.getValue("lengthmax"), 1 * 1024 * 1024L);
this.watch = cacheconf.getBoolValue("watch", false);
} }
List<SimpleEntry<Pattern, String>> locations = new ArrayList<>(); List<SimpleEntry<Pattern, String>> locations = new ArrayList<>();
for (AnyValue av : config.getAnyValues("rewrite")) { for (AnyValue av : config.getAnyValues("rewrite")) {
@@ -135,7 +140,7 @@ public final class HttpResourceServlet extends HttpServlet {
this.locationRewrites = locations.isEmpty() ? null : locations.toArray(new SimpleEntry[locations.size()]); this.locationRewrites = locations.isEmpty() ? null : locations.toArray(new SimpleEntry[locations.size()]);
} }
if (this.cachelimit < 1) return; //不缓存不需要开启WatchThread监听 if (this.cachelimit < 1) return; //不缓存不需要开启WatchThread监听
if (this.root != null) { if (this.root != null && this.watch) {
try { try {
this.watchThread = new WatchThread(this.root); this.watchThread = new WatchThread(this.root);
this.watchThread.start(); this.watchThread.start();