summaryrefslogtreecommitdiff
path: root/internal
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <saya.andy@posteo.com> 2025-09-03 17:34:25 +0700
committerGravatar SayaAndy <saya.andy@posteo.com> 2025-09-03 17:34:25 +0700
commitec82d5586e2d39a54f38d97a35641c1785406661 (patch)
tree0fdeecdd71d6310e908ec61fac50841f151ab3ef /internal
parent55b399a839d58754b0487a5e20d077a1e8f5d38d (diff)
downloadweb-ec82d5586e2d39a54f38d97a35641c1785406661.tar.gz
web-ec82d5586e2d39a54f38d97a35641c1785406661.zip
feat: blog search pages cache
Diffstat (limited to 'internal')
-rw-r--r--internal/router/api-v1-blog-search.go16
1 files changed, 12 insertions, 4 deletions
diff --git a/internal/router/api-v1-blog-search.go b/internal/router/api-v1-blog-search.go
index e22fabe..41f2dec 100644
--- a/internal/router/api-v1-blog-search.go
+++ b/internal/router/api-v1-blog-search.go
@@ -1,6 +1,7 @@
package router
import (
+ "encoding/json"
"fmt"
"log/slog"
"net/url"
@@ -35,10 +36,17 @@ func Api_V1_BlogSearch(l map[string]*locale.LocaleConfig, langs []string, b2Clie
slog.Warn("unable to parse a client timezone, defaulting to UTC", slog.String("error", err.Error()), slog.String("tz", tz))
}
- pages, err := b2Client.Scan(lang + "/")
- if err != nil {
- c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
- return c.Status(fiber.ErrInternalServerError.Code).SendString(fmt.Sprintf("failed to scan pages for '%s' lang: %v", lang, err))
+ var pages []*b2.BlogPage
+ if pagesBytes, ok := PCache.Get("blog-search.pages-list"); pagesBytes != nil || ok {
+ json.Unmarshal(pagesBytes, &pages)
+ } else {
+ pages, err = b2Client.Scan(lang + "/")
+ if err != nil {
+ c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
+ return c.Status(fiber.ErrInternalServerError.Code).SendString(fmt.Sprintf("failed to scan pages for '%s' lang: %v", lang, err))
+ }
+ pagesBytes, _ := json.Marshal(pages)
+ PCache.SetWithTTL("blog-search.pages-list", pagesBytes, int64(len(pagesBytes)), 5*time.Minute)
}
encodedQuery := c.Request().URI().QueryString()