write about caching

This commit is contained in:
iacore
2024-08-27 20:20:02 +00:00
parent c4d8fdb0f0
commit 0d04402d2c
4 changed files with 37 additions and 3 deletions
+6
View File
@@ -0,0 +1,6 @@
package config
import "time"
// todo: make this configurable
const ExpiresIn = 5 * time.Minute
+22
View File
@@ -14,3 +14,25 @@
- add limiter (maybe it should be in nginx)
- add caching (maybe it should be in nginx)
## Guide for caching
The majority of the site's traffic is images, and all images on the Pixiv route can be cached forever.
Cached in browser: pixiv headers cache images for year
On the server, the reverse proxy (e.g. nginx) can be cached worry-free forever.
JSON requests to Pixiv can be cached for some time.
todo: We should have a middleware handling that.
Assets are already cached properly thanks to net/http.
Every rendered page can be cached for 5 minutes or so in the browser.
However, after changing settings, the page need to be refreshed instantly.
Cache in browser: todo: read below implement in template/render.go
The reverse proxy shouldn't do anything about this.
The only three headers that controls caching in browser and middleboxes:
- [Age](https://httpwg.org/specs/rfc9111.html#field.age)
- [Cache-Control](https://httpwg.org/specs/rfc9111.html#field.cache-control)
- [Expires](https://httpwg.org/specs/rfc9111.html#field.expires)
+5 -3
View File
@@ -35,7 +35,10 @@ func CanRequestSkipLimiter(r *http.Request) bool {
func CanRequestSkipLogger(r *http.Request) bool {
// return false
path := r.URL.Path
return CanRequestSkipLimiter(r) ||
return strings.HasPrefix(path, "/img/") ||
strings.HasPrefix(path, "/css/") ||
strings.HasPrefix(path, "/js/") ||
strings.HasPrefix(path, "/proxy/s.pximg.net/") ||
strings.HasPrefix(path, "/proxy/i.pximg.net/")
}
@@ -82,7 +85,6 @@ func main() {
router.ServeHTTP(w, r)
}
CatchError(func(w http.ResponseWriter, r *http.Request) error {
err := GetUserContext(r).err
if err != nil { // error handler
@@ -228,7 +230,7 @@ func defineRoutes() *mux.Router {
// Legacy illust URL
router.HandleFunc("/member_illust.php", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/artworks/"+ routes.GetQueryParam(r, "illust_id"), http.StatusPermanentRedirect)
http.Redirect(w, r, "/artworks/"+routes.GetQueryParam(r, "illust_id"), http.StatusPermanentRedirect)
}).Methods("GET")
router.NewRoute().HandlerFunc(CatchError(func(w http.ResponseWriter, r *http.Request) error {
+4
View File
@@ -6,7 +6,9 @@ import (
"net/http"
"reflect"
"strings"
// "time"
// "codeberg.org/vnpower/pixivfe/v2/config"
"codeberg.org/vnpower/pixivfe/v2/session"
"codeberg.org/vnpower/pixivfe/v2/utils"
@@ -35,6 +37,8 @@ func InitTemplatingEngine(DisableCache bool) {
// render the template selected based on the name of type `T`
func Render[T any](w http.ResponseWriter, r *http.Request, data T) error {
w.Header().Set("content-type", "text/html; charset=utf-8")
// todo: think about caching a bit more
// w.Header().Set("expires", time.Now().Add(config.ExpiresIn).Format(time.RFC1123))
return RenderInner(w, GetTemplatingVariables(r), data)
}