summaryrefslogtreecommitdiffci
path: root/internal/router/api-v1-general-page-body.go
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
authorGravatar SayaAndy <saya.andy@posteo.com> 2025-08-31 18:40:13 +0700
committerGravatar SayaAndy <saya.andy@posteo.com> 2025-08-31 18:40:13 +0700
commit6af14241ff2c8050753f1d6ac702a938a017a839 (patch)
tree29400fa749d7bc87ed30cc924e486791f8f72867 /internal/router/api-v1-general-page-body.go
parentf174579ef53fb726594a2f7064299b86c8691ea3 (diff)
downloadweb-6af14241ff2c8050753f1d6ac702a938a017a839.tar.gz
web-6af14241ff2c8050753f1d6ac702a938a017a839.zip
feat: add page cache
Diffstat (limited to 'internal/router/api-v1-general-page-body.go')
-rw-r--r--internal/router/api-v1-general-page-body.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/internal/router/api-v1-general-page-body.go b/internal/router/api-v1-general-page-body.go
index 4108a4f..2329c4a 100644
--- a/internal/router/api-v1-general-page-body.go
+++ b/internal/router/api-v1-general-page-body.go
@@ -8,6 +8,7 @@ import (
"regexp"
"slices"
"strings"
+ "time"
"github.com/SayaAndy/saya-today-web/internal/b2"
"github.com/SayaAndy/saya-today-web/locale"
@@ -33,6 +34,13 @@ func Api_V1_GeneralPage_Body(l map[string]*locale.LocaleConfig, langs []string,
}
path := urlStruct.EscapedPath()
+
+ cacheKey := fmt.Sprintf("body.%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}/...'")
@@ -133,6 +141,7 @@ func Api_V1_GeneralPage_Body(l map[string]*locale.LocaleConfig, langs []string,
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)
}