Skip to content

Commit

Permalink
Merge pull request #140 from Lixuhuilll/add-http-cache
Browse files Browse the repository at this point in the history
Add HTTP Etag Cache
  • Loading branch information
dragove authored Oct 20, 2023
2 parents 9cf327f + f421ed4 commit fa27e7a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/plus/maa/backend/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand All @@ -17,6 +18,7 @@
@SpringBootApplication
@ConfigurationPropertiesScan
@EnableMethodSecurity
@ServletComponentScan
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import plus.maa.backend.common.annotation.JsonSchema;
import plus.maa.backend.common.annotation.SensitiveWordDetection;
Expand All @@ -33,6 +35,7 @@
public class CopilotController {
private final CopilotService copilotService;
private final AuthenticationHelper helper;
private final HttpServletResponse response;

@Operation(summary = "上传作业")
@ApiResponse(description = "上传作业结果")
Expand Down Expand Up @@ -75,6 +78,8 @@ public MaaResult<CopilotInfo> getCopilotById(
public MaaResult<CopilotPageInfo> queriesCopilot(
@Parameter(description = "作业查询请求") @Valid CopilotQueriesRequest parsed
) {
// 两秒防抖,缓解前端重复请求问题
response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=2, must-revalidate");
return MaaResult.success(copilotService.queriesCopilot(helper.getUserId(), parsed));
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package plus.maa.backend.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

import java.io.IOException;


/**
* 提供基于 Etag 机制的 HTTP 缓存,有助于降低网络传输的压力
*
* @author lixuhuilll
*/

// 配置需要使用 Etag 机制的 URL,注意和 Spring 的 UrlPattern 语法不太一样
@WebFilter(urlPatterns = {
"/arknights/level",
"/copilot/query"
})
@RequiredArgsConstructor
public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {

@Override
protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException {
if (!HttpMethod.GET.matches(request.getMethod())) {
// ETag 只处理安全的请求
filterChain.doFilter(request, response);
return;
}
// 允许使用 Etag (实际是避免默认添加的 no-store)
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, max-age=0, must-revalidate");
// 其他接口默认处理即可,注意默认操作相当于牺牲 CPU 来节约网络带宽,不适用于结果变更过快的接口
super.doFilterInternal(request, response, filterChain);
}
}

0 comments on commit fa27e7a

Please sign in to comment.