Cache Key Gen

This commit is contained in:
Simon Martens
2025-04-14 13:26:19 +02:00
parent 31d40c1ce1
commit 113a084f50
4 changed files with 14 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package server
import ( import (
"time" "time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/storage/memory/v2" "github.com/gofiber/storage/memory/v2"
) )
@@ -17,5 +18,10 @@ const (
func NewCache() Cache { func NewCache() Cache {
return Cache{memory.New(memory.Config{ return Cache{memory.New(memory.Config{
GCInterval: CACHE_GC_INTERVAL, GCInterval: CACHE_GC_INTERVAL,
})} }),
}
}
func KeyGenerator(c *fiber.Ctx) string {
return c.Path() + c.Context().QueryArgs().String()
} }

View File

@@ -53,6 +53,7 @@ func New(engine *templating.Engine, storage fiber.Storage, debug bool) Server {
Expiration: CACHE_TIME, Expiration: CACHE_TIME,
CacheControl: false, CacheControl: false,
Storage: storage, Storage: storage,
KeyGenerator: KeyGenerator,
})) }))
server.Use(logger.New()) server.Use(logger.New())
} }

View File

@@ -189,7 +189,7 @@ func (e *Engine) AddFuncs(funcs template.FuncMap) {
} }
func (e *Engine) Render(out io.Writer, path string, data any, layout ...string) error { func (e *Engine) Render(out io.Writer, path string, data any, layout ...string) error {
slog.Debug("Rendering", "path", path, "layout", layout, "data", data) // slog.Debug("Rendering", "path", path, "layout", layout, "data", data)
e.mu.RLock() e.mu.RLock()
ld := data.(fiber.Map) ld := data.(fiber.Map)
if e.GlobalData != nil { if e.GlobalData != nil {

View File

@@ -74,9 +74,11 @@ func Iterate[T any](xmlData string, initialState T) iter.Seq2[*TokenResult[T], e
} }
customToken = Token{Name: t.Name.Local, Inner: t, Type: EndElement} customToken = Token{Name: t.Name.Local, Inner: t, Type: EndElement}
case xml.CharData: case xml.CharData:
text := strings.TrimSpace(string(t)) text := string(t)
if text != "" && len(stack) > 0 { if text != "" && len(stack) > 0 {
stack[len(stack)-1].CharData += text + " " for i := range stack {
stack[i].CharData += text
}
} }
customToken = Token{ customToken = Token{
Name: "CharData", Name: "CharData",
@@ -110,7 +112,7 @@ func Iterate[T any](xmlData string, initialState T) iter.Seq2[*TokenResult[T], e
result := &TokenResult[T]{ result := &TokenResult[T]{
State: state, State: state,
Token: customToken, Token: customToken,
Stack: append([]Element{}, stack...), Stack: stack,
} }
if !yield(result, nil) { if !yield(result, nil) {