diff options
| author | 2025-08-31 18:40:13 +0700 | |
|---|---|---|
| committer | 2025-08-31 18:40:13 +0700 | |
| commit | 6af14241ff2c8050753f1d6ac702a938a017a839 (patch) | |
| tree | 29400fa749d7bc87ed30cc924e486791f8f72867 /internal | |
| parent | f174579ef53fb726594a2f7064299b86c8691ea3 (diff) | |
| download | web-6af14241ff2c8050753f1d6ac702a938a017a839.tar.gz web-6af14241ff2c8050753f1d6ac702a938a017a839.zip | |
feat: add page cache
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/router/api-v1-general-page-body.go | 9 | ||||
| -rw-r--r-- | internal/router/api-v1-general-page-bottom-embeds.go | 8 | ||||
| -rw-r--r-- | internal/router/api-v1-general-page-footer.go | 9 | ||||
| -rw-r--r-- | internal/router/api-v1-general-page-header.go | 9 | ||||
| -rw-r--r-- | internal/router/api-v1-general-page-top-embeds.go | 9 | ||||
| -rw-r--r-- | internal/router/client-cache.go | 5 | ||||
| -rw-r--r-- | internal/router/page-cache.go | 5 |
7 files changed, 49 insertions, 5 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) } 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) } diff --git a/internal/router/api-v1-general-page-footer.go b/internal/router/api-v1-general-page-footer.go index 39b6bea..98da6bb 100644 --- a/internal/router/api-v1-general-page-footer.go +++ b/internal/router/api-v1-general-page-footer.go @@ -6,6 +6,7 @@ import ( "net/url" "slices" "strings" + "time" "github.com/SayaAndy/saya-today-web/locale" "github.com/gofiber/fiber/v2" @@ -29,6 +30,13 @@ func Api_V1_GeneralPage_Footer(l map[string]*locale.LocaleConfig, langs []string } path := urlStruct.EscapedPath() + + cacheKey := fmt.Sprintf("footer.%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}/...'") @@ -58,6 +66,7 @@ func Api_V1_GeneralPage_Footer(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) } diff --git a/internal/router/api-v1-general-page-header.go b/internal/router/api-v1-general-page-header.go index 0529ba6..1d65966 100644 --- a/internal/router/api-v1-general-page-header.go +++ b/internal/router/api-v1-general-page-header.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,13 @@ func Api_V1_GeneralPage_Header(l map[string]*locale.LocaleConfig, langs []string } path := urlStruct.EscapedPath() + + cacheKey := fmt.Sprintf("header.%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}/...'") @@ -67,6 +75,7 @@ func Api_V1_GeneralPage_Header(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) } diff --git a/internal/router/api-v1-general-page-top-embeds.go b/internal/router/api-v1-general-page-top-embeds.go index 090107b..b26675f 100644 --- a/internal/router/api-v1-general-page-top-embeds.go +++ b/internal/router/api-v1-general-page-top-embeds.go @@ -6,6 +6,7 @@ import ( "net/url" "slices" "strings" + "time" "github.com/SayaAndy/saya-today-web/locale" "github.com/gofiber/fiber/v2" @@ -29,6 +30,13 @@ func Api_V1_GeneralPage_TopEmbeds(l map[string]*locale.LocaleConfig, langs []str } path := urlStruct.EscapedPath() + + cacheKey := fmt.Sprintf("top-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}/...'") @@ -58,6 +66,7 @@ func Api_V1_GeneralPage_TopEmbeds(l map[string]*locale.LocaleConfig, langs []str 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) } diff --git a/internal/router/client-cache.go b/internal/router/client-cache.go index 81d7641..e448fb3 100644 --- a/internal/router/client-cache.go +++ b/internal/router/client-cache.go @@ -11,11 +11,6 @@ import ( "golang.org/x/crypto/argon2" ) -type PageLike struct { - PageRef string - UserId string -} - type ClientCache struct { hashMap map[string]string hashMapMutex sync.RWMutex diff --git a/internal/router/page-cache.go b/internal/router/page-cache.go new file mode 100644 index 0000000..01e4792 --- /dev/null +++ b/internal/router/page-cache.go @@ -0,0 +1,5 @@ +package router + +import "github.com/dgraph-io/ristretto/v2" + +var PCache *ristretto.Cache[string, []byte] |