Projects list cache
This commit is contained in:
parent
505e5aae75
commit
1a957145e8
|
@ -23,29 +23,27 @@ func projectsRouter(router fiber.Router) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
var projectsLastEdit time.Time;
|
||||
var projectsLastEdit time.Time = time.Now().Truncate(time.Second);
|
||||
|
||||
func GetAllProjects(c *fiber.Ctx) error {
|
||||
isCached, err := utils.Cache(c, projectsLastEdit);
|
||||
isCached, err := utils.Cache(c, &projectsLastEdit);
|
||||
if err == nil && isCached {
|
||||
return nil;
|
||||
}
|
||||
|
||||
projects, err := db.GetAllProjects()
|
||||
currentTime := time.Now();
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Cannot retrieve projects",
|
||||
"trace": fmt.Sprint(err),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
err = c.Status(fiber.StatusOK).JSON(projects)
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
projectsLastEdit = currentTime;
|
||||
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{
|
||||
"id": id,
|
||||
})
|
||||
|
@ -111,6 +111,8 @@ func UpdateProject(c *fiber.Ctx) error {
|
|||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
projectsLastEdit = time.Now().Truncate(time.Second);
|
||||
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
|
||||
|
@ -132,6 +134,8 @@ func DeleteProject(c *fiber.Ctx) error {
|
|||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
projectsLastEdit = time.Now().Truncate(time.Second);
|
||||
|
||||
return c.SendStatus(fiber.StatusNoContent)
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ func main() {
|
|||
app.Use(cors.New(cors.Config{
|
||||
AllowOrigins: origins,
|
||||
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())
|
||||
|
|
|
@ -6,16 +6,22 @@ import (
|
|||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Cache(c *fiber.Ctx, lastEdit time.Time) (bool, error) {
|
||||
clientLast, err := time.Parse(time.RFC1123, c.Get("If-Modified-Since"))
|
||||
func Cache(c *fiber.Ctx, lastEdit *time.Time) (bool, error) {
|
||||
ifModifiedSince := c.Get("If-Modified-Since")
|
||||
if ifModifiedSince == "" {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
clientLast, err := time.Parse(time.RFC1123, ifModifiedSince)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if clientLast.Before(lastEdit) {
|
||||
|
||||
if !lastEdit.After(clientLast) {
|
||||
c.SendStatus(fiber.StatusNotModified)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
c.Set("Last-Modified", lastEdit.Format(time.RFC1123))
|
||||
return false, nil
|
||||
}
|
Loading…
Reference in New Issue