mirror of
https://github.com/wushuo894/ani-rss.git
synced 2026-03-13 09:20:23 +00:00
refactor: 优化页面缓存
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
package ani.rss.config;
|
||||
|
||||
import ani.rss.entity.Global;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.servlet.*;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static ani.rss.controller.BaseController.setCacheControl;
|
||||
|
||||
@Component
|
||||
public class WebFilter implements Filter {
|
||||
/**
|
||||
* 指定缓存的文件
|
||||
*/
|
||||
private static final List<String> CACHE_EXT = List.of("css", "js", "jpg", "png", "svg", "ico");
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
|
||||
HttpServletRequest request = (HttpServletRequest) req;
|
||||
@@ -17,10 +27,21 @@ public class WebFilter implements Filter {
|
||||
|
||||
String uri = request.getRequestURI();
|
||||
|
||||
if (!uri.startsWith("/api") && !uri.contains(".") && !uri.endsWith("/")) {
|
||||
String htmlPath = uri + ".html";
|
||||
request.getRequestDispatcher(htmlPath).forward(request, response);
|
||||
return;
|
||||
// 非 api
|
||||
if (!uri.startsWith("/api")) {
|
||||
String extName = FileUtil.extName(uri);
|
||||
|
||||
if (StrUtil.isBlank(extName) && !uri.endsWith("/")) {
|
||||
String htmlPath = uri + ".html";
|
||||
request.getRequestDispatcher(htmlPath).forward(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (StrUtil.isNotBlank(extName) && CACHE_EXT.contains(extName)) {
|
||||
setCacheControl(response, 86400);
|
||||
} else {
|
||||
setCacheControl(response, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Global.REQUEST.set(request);
|
||||
|
||||
@@ -6,6 +6,7 @@ import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.Cleanup;
|
||||
@@ -42,6 +43,17 @@ public class BaseController {
|
||||
return ContentType.OCTET_STREAM.getValue();
|
||||
}
|
||||
|
||||
public static void setCacheControl(HttpServletResponse response, long maxAge) {
|
||||
if (maxAge > 0) {
|
||||
response.setHeader(Header.CACHE_CONTROL.toString(), "private, max-age=" + maxAge);
|
||||
return;
|
||||
}
|
||||
|
||||
response.setHeader(Header.CACHE_CONTROL.toString(), "no-store, no-cache, must-revalidate, max-age=0");
|
||||
response.setHeader(Header.PRAGMA.toString(), "no-cache");
|
||||
response.setHeader("Expires", "0");
|
||||
}
|
||||
|
||||
public static void writeNotFound() {
|
||||
writeHtml(HttpStatus.HTTP_NOT_FOUND, "404 Not Found !");
|
||||
}
|
||||
@@ -53,6 +65,7 @@ public class BaseController {
|
||||
try {
|
||||
response.setStatus(status);
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
response.setContentLength(html.length());
|
||||
|
||||
@Cleanup
|
||||
OutputStream outputStream = response.getOutputStream();
|
||||
|
||||
@@ -257,9 +257,7 @@ public class ConfigController extends BaseController {
|
||||
@GetMapping("/custom.js")
|
||||
public void customJs() throws IOException {
|
||||
HttpServletResponse response = Global.RESPONSE.get();
|
||||
response.setHeader(Header.CACHE_CONTROL.toString(), "no-store, no-cache, must-revalidate, max-age=0");
|
||||
response.setHeader(Header.PRAGMA.toString(), "no-cache");
|
||||
response.setHeader("Expires", "0");
|
||||
setCacheControl(response, 0);
|
||||
|
||||
String customJs = ConfigUtil.CONFIG.getCustomJs();
|
||||
customJs = StrUtil.blankToDefault(customJs, "// empty js");
|
||||
@@ -276,9 +274,7 @@ public class ConfigController extends BaseController {
|
||||
@GetMapping("/custom.css")
|
||||
public void customCss() throws IOException {
|
||||
HttpServletResponse response = Global.RESPONSE.get();
|
||||
response.setHeader(Header.CACHE_CONTROL.toString(), "no-store, no-cache, must-revalidate, max-age=0");
|
||||
response.setHeader(Header.PRAGMA.toString(), "no-cache");
|
||||
response.setHeader("Expires", "0");
|
||||
setCacheControl(response, 0);
|
||||
|
||||
String customCss = ConfigUtil.CONFIG.getCustomCss();
|
||||
customCss = StrUtil.blankToDefault(customCss, "/* empty css */");
|
||||
|
||||
@@ -94,7 +94,7 @@ public class FileController extends BaseController {
|
||||
maxAge = 86400 * 30;
|
||||
}
|
||||
|
||||
response.setHeader(Header.CACHE_CONTROL.toString(), "private, max-age=" + maxAge);
|
||||
setCacheControl(response, maxAge);
|
||||
response.setContentType(contentType);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpConnection;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.servlet.ServletOutputStream;
|
||||
@@ -101,8 +100,7 @@ public class MikanController extends BaseController {
|
||||
|
||||
// 30 天
|
||||
long maxAge = 86400 * 30;
|
||||
|
||||
response.setHeader(Header.CACHE_CONTROL.toString(), "private, max-age=" + maxAge);
|
||||
setCacheControl(response, maxAge);
|
||||
|
||||
String contentType = getContentType(URLUtil.getPath(img));
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ spring:
|
||||
static-locations: classpath:/META-INF/dist
|
||||
chain:
|
||||
compressed: true
|
||||
add-mappings: false
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
time-zone: GMT+8
|
||||
|
||||
Reference in New Issue
Block a user