Projects list cache

This commit is contained in:
Brieuc Dubois 2024-01-04 16:20:00 +01:00
parent 505e5aae75
commit 1a957145e8
3 changed files with 20 additions and 10 deletions

View File

@ -23,16 +23,15 @@ func projectsRouter(router fiber.Router) error {
return nil return nil
} }
var projectsLastEdit time.Time; var projectsLastEdit time.Time = time.Now().Truncate(time.Second);
func GetAllProjects(c *fiber.Ctx) error { func GetAllProjects(c *fiber.Ctx) error {
isCached, err := utils.Cache(c, projectsLastEdit); isCached, err := utils.Cache(c, &projectsLastEdit);
if err == nil && isCached { if err == nil && isCached {
return nil; return nil;
} }
projects, err := db.GetAllProjects() projects, err := db.GetAllProjects()
currentTime := time.Now();
if err != nil { if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot retrieve projects", "error": "Cannot retrieve projects",
@ -45,7 +44,6 @@ func GetAllProjects(c *fiber.Ctx) error {
return err; return err;
} }
projectsLastEdit = currentTime;
return nil; return nil;
} }
@ -83,6 +81,8 @@ func CreateProject(c *fiber.Ctx) error {
}) })
} }
projectsLastEdit = time.Now().Truncate(time.Second);
return c.Status(fiber.StatusCreated).JSON(fiber.Map{ return c.Status(fiber.StatusCreated).JSON(fiber.Map{
"id": id, "id": id,
}) })
@ -111,6 +111,8 @@ func UpdateProject(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusNotFound) return c.SendStatus(fiber.StatusNotFound)
} }
projectsLastEdit = time.Now().Truncate(time.Second);
return c.SendStatus(fiber.StatusNoContent) return c.SendStatus(fiber.StatusNoContent)
} }
@ -132,6 +134,8 @@ func DeleteProject(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusNotFound) return c.SendStatus(fiber.StatusNotFound)
} }
projectsLastEdit = time.Now().Truncate(time.Second);
return c.SendStatus(fiber.StatusNoContent) return c.SendStatus(fiber.StatusNoContent)
} }

View File

@ -33,7 +33,7 @@ func main() {
app.Use(cors.New(cors.Config{ app.Use(cors.New(cors.Config{
AllowOrigins: origins, AllowOrigins: origins,
AllowMethods: "GET,POST,PUT,DELETE", AllowMethods: "GET,POST,PUT,DELETE",
AllowHeaders: "Origin, Content-Type, Accept", AllowHeaders: "Origin, Content-Type, Accept, Cache-Control, Pragma,Expires, If-Modified-Since",
})) }))
// app.Use(cache.New()) // app.Use(cache.New())

View File

@ -6,16 +6,22 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func Cache(c *fiber.Ctx, lastEdit time.Time) (bool, error) { func Cache(c *fiber.Ctx, lastEdit *time.Time) (bool, error) {
clientLast, err := time.Parse(time.RFC1123, c.Get("If-Modified-Since")) ifModifiedSince := c.Get("If-Modified-Since")
if ifModifiedSince == "" {
return false, nil
}
clientLast, err := time.Parse(time.RFC1123, ifModifiedSince)
if err != nil { if err != nil {
return false, err return false, err
} }
if clientLast.Before(lastEdit) { if !lastEdit.After(clientLast) {
c.SendStatus(fiber.StatusNotModified) c.SendStatus(fiber.StatusNotModified)
return true, nil return true, nil
} }
c.Set("Last-Modified", lastEdit.Format(time.RFC1123))
return false, nil return false, nil
} }