From 2b1c8c6da445f827ca7e4f6031f36ee1f900d975 Mon Sep 17 00:00:00 2001 From: GZTime Date: Sun, 19 Jan 2025 17:33:18 +0800 Subject: [PATCH] feat(cache): add caching for most apis that will rarely change --- src/GZCTF/Controllers/GameController.cs | 1 + src/GZCTF/Controllers/InfoController.cs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/GZCTF/Controllers/GameController.cs b/src/GZCTF/Controllers/GameController.cs index 0df69f594..d9a7658f6 100644 --- a/src/GZCTF/Controllers/GameController.cs +++ b/src/GZCTF/Controllers/GameController.cs @@ -58,6 +58,7 @@ public class GameController( /// /// Successfully retrieved game information [HttpGet("Recent")] + [ResponseCache(Duration = 300)] [ProducesResponseType(typeof(BasicGameInfoModel[]), StatusCodes.Status200OK)] public async Task RecentGames( [FromQuery][Range(0, 50)] int limit, diff --git a/src/GZCTF/Controllers/InfoController.cs b/src/GZCTF/Controllers/InfoController.cs index 4998cc048..ba82353e7 100644 --- a/src/GZCTF/Controllers/InfoController.cs +++ b/src/GZCTF/Controllers/InfoController.cs @@ -40,6 +40,7 @@ public class InfoController( /// /// Successfully retrieved posts [HttpGet("Posts/Latest")] + [ResponseCache(Duration = 300)] [ProducesResponseType(typeof(PostInfoModel[]), StatusCodes.Status200OK)] public async Task GetLatestPosts(CancellationToken token) => Ok((await postRepository.GetPosts(token)).Take(20).Select(PostInfoModel.FromPost)); @@ -53,6 +54,8 @@ public async Task GetLatestPosts(CancellationToken token) => /// /// Successfully retrieved posts [HttpGet("Posts")] + [ResponseCache(Duration = 300)] + [EnableRateLimiting(nameof(RateLimiter.LimitPolicy.Query))] [ProducesResponseType(typeof(PostInfoModel[]), StatusCodes.Status200OK)] public async Task GetPosts(CancellationToken token) => Ok((await postRepository.GetPosts(token)).Select(PostInfoModel.FromPost)); @@ -68,6 +71,7 @@ public async Task GetPosts(CancellationToken token) => /// Successfully retrieved post details /// Post not found [HttpGet("Posts/{id}")] + [ResponseCache(Duration = 300)] [ProducesResponseType(typeof(PostDetailModel), StatusCodes.Status200OK)] [ProducesResponseType(typeof(RequestResponse), StatusCodes.Status404NotFound)] public async Task GetPost(string id, CancellationToken token) @@ -89,6 +93,7 @@ public async Task GetPost(string id, CancellationToken token) /// /// Successfully retrieved configuration [HttpGet("Config")] + [ResponseCache(Duration = 300)] [ProducesResponseType(typeof(ClientConfig), StatusCodes.Status200OK)] public async Task GetClientConfig(CancellationToken token = default) { @@ -111,6 +116,7 @@ public async Task GetClientConfig(CancellationToken token = defau /// /// Successfully retrieved Captcha configuration [HttpGet("Captcha")] + [ResponseCache(Duration = 300)] [ProducesResponseType(typeof(ClientCaptchaInfoModel), StatusCodes.Status200OK)] public async Task GetClientCaptchaInfo(CancellationToken token = default) {