From 6af14241ff2c8050753f1d6ac702a938a017a839 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sun, 31 Aug 2025 18:40:13 +0700 Subject: feat: add page cache --- internal/router/api-v1-general-page-bottom-embeds.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'internal/router/api-v1-general-page-bottom-embeds.go') diff --git a/internal/router/api-v1-general-page-bottom-embeds.go b/internal/router/api-v1-general-page-bottom-embeds.go index f0957f3..df766a7 100644 --- a/internal/router/api-v1-general-page-bottom-embeds.go +++ b/internal/router/api-v1-general-page-bottom-embeds.go @@ -6,6 +6,7 @@ import ( "net/url" "slices" "strings" + "time" "github.com/SayaAndy/saya-today-web/internal/b2" "github.com/SayaAndy/saya-today-web/locale" @@ -30,6 +31,12 @@ func Api_V1_GeneralPage_BottomEmbeds(l map[string]*locale.LocaleConfig, langs [] } path := urlStruct.EscapedPath() + cacheKey := fmt.Sprintf("bottom-embeds.%s", path) + if val, ok := PCache.Get(cacheKey); val != nil && ok { + c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8) + return c.Status(fiber.StatusOK).Type("html").Send(val) + } + pathParts := strings.Split(strings.Trim(path, "/"), "/") if len(pathParts) < 2 { return c.Status(fiber.ErrBadRequest.Code).SendString("'Referer' header is invalid: expect format '/{lang}/...'") @@ -59,6 +66,7 @@ func Api_V1_GeneralPage_BottomEmbeds(l map[string]*locale.LocaleConfig, langs [] return c.Status(fiber.ErrInternalServerError.Code).SendString("failed to generate div") } + go PCache.SetWithTTL(cacheKey, content, int64(len(content)), 5*time.Minute) c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8) return c.Status(fiber.StatusOK).Type("html").Send(content) } -- cgit v1.3.1+17