perf: cache total jokes!!

This commit is contained in:
Reinaldy Rafli 2021-07-19 17:21:08 +07:00
parent abbef79544
commit 2830a483be
7 changed files with 100 additions and 35 deletions

View File

@ -44,21 +44,14 @@ func New() *fiber.App {
log.Fatal(err) log.Fatal(err)
} }
checkCache, err := core.CheckJokesCache(memory) err = core.SetAllJSONJoke(db, memory)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
err = core.SetTotalJoke(db, memory)
if !checkCache {
jokes, err := core.GetAllJSONJokes(db)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
err = memory.Set("jokes", jokes)
if err != nil {
log.Fatalln(err)
}
}
app.Use(cors.New()) app.Use(cors.New())
app.Use(etag.New()) app.Use(etag.New())

View File

@ -74,6 +74,19 @@ func CheckJokesCache(memory *bigcache.BigCache) (bool, error) {
return true, nil return true, nil
} }
// CheckTotalJokesCache literally does what the name is for
func CheckTotalJokesCache(memory *bigcache.BigCache) (bool, error) {
_, err := memory.Get("total")
if err != nil {
if err.Error() == "Entry not found" {
return false, nil
}
return false, err
}
return true, nil
}
// GetCachedJokeByID returns a link string of a certain ID from cache. // GetCachedJokeByID returns a link string of a certain ID from cache.
func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) { func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) {
jokes, err := memory.Get("jokes") jokes, err := memory.Get("jokes")
@ -99,3 +112,16 @@ func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) {
return "", nil return "", nil
} }
// GetCachedTotalJokes
func GetCachedTotalJokes(memory *bigcache.BigCache) (int, error) {
total, err := memory.Get("total")
if err != nil {
if err.Error() == "Entry not found" {
return 0, models.ErrNotFound
}
return 0, err
}
return int(total[0]), nil
}

View File

@ -0,0 +1,57 @@
package core
import (
"jokes-bapak2-api/app/v1/models"
"github.com/allegro/bigcache/v3"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pquerna/ffjson/ffjson"
)
// SetAllJSONJoke fetches jokes data from GetAllJSONJokes then set it to memory cache.
func SetAllJSONJoke(db *pgxpool.Pool, memory *bigcache.BigCache) error {
jokes, err := GetAllJSONJokes(db)
if err != nil {
return err
}
err = memory.Set("jokes", jokes)
if err != nil {
return err
}
return nil
}
func SetTotalJoke(db *pgxpool.Pool, memory *bigcache.BigCache) error {
check, err := CheckJokesCache(memory)
if err != nil {
return err
}
if !check {
return models.ErrEmpty
}
err = SetAllJSONJoke(db, memory)
if err != nil {
return err
}
jokes, err := memory.Get("jokes")
if err != nil {
return err
}
var data []models.Joke
err = ffjson.Unmarshal(jokes, &data)
if err != nil {
return err
}
var total = []byte{byte(len(data))}
err = memory.Set("total", total)
if err != nil {
return err
}
return nil
}

View File

@ -27,11 +27,11 @@ func AddNewJoke(c *fiber.Ctx) error {
return err return err
} }
jokes, err := core.GetAllJSONJokes(db) err = core.SetAllJSONJoke(db, memory)
if err != nil { if err != nil {
return err return err
} }
err = memory.Set("jokes", jokes) err = core.SetTotalJoke(db, memory)
if err != nil { if err != nil {
return err return err
} }

View File

@ -40,11 +40,11 @@ func DeleteJoke(c *fiber.Ctx) error {
return err return err
} }
jokes, err := core.GetAllJSONJokes(db) err = core.SetAllJSONJoke(db, memory)
if err != nil { if err != nil {
return err return err
} }
err = memory.Set("jokes", jokes) err = core.SetTotalJoke(db, memory)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,30 +3,27 @@ package handler
import ( import (
"jokes-bapak2-api/app/v1/core" "jokes-bapak2-api/app/v1/core"
"jokes-bapak2-api/app/v1/models" "jokes-bapak2-api/app/v1/models"
"log"
"strconv" "strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/pquerna/ffjson/ffjson"
) )
func TotalJokes(c *fiber.Ctx) error { func TotalJokes(c *fiber.Ctx) error {
checkCache, err := core.CheckJokesCache(memory) checkTotal, err := core.CheckTotalJokesCache(memory)
if err != nil { if err != nil {
return err return err
} }
if !checkCache { if !checkTotal {
jokes, err := core.GetAllJSONJokes(db) err = core.SetTotalJoke(db, memory)
if err != nil {
return err
}
err = memory.Set("jokes", jokes)
if err != nil { if err != nil {
return err return err
} }
} }
jokes, err := memory.Get("jokes") total, err := memory.Get("total")
log.Println(err)
if err != nil { if err != nil {
if err.Error() == "Entry not found" { if err.Error() == "Entry not found" {
return c.Status(fiber.StatusInternalServerError).JSON(models.Error{ return c.Status(fiber.StatusInternalServerError).JSON(models.Error{
@ -36,14 +33,7 @@ func TotalJokes(c *fiber.Ctx) error {
return err return err
} }
var data []models.Joke
err = ffjson.Unmarshal(jokes, &data)
if err != nil {
return err
}
dataLength := strconv.Itoa(len(data))
return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{ return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{
Message: dataLength, Message: strconv.Itoa(int(total[0])),
}) })
} }

View File

@ -41,12 +41,11 @@ func UpdateJoke(c *fiber.Ctx) error {
return err return err
} }
jokes, err := core.GetAllJSONJokes(db) err = core.SetAllJSONJoke(db, memory)
if err != nil { if err != nil {
return err return err
} }
err = core.SetTotalJoke(db, memory)
err = memory.Set("jokes", jokes)
if err != nil { if err != nil {
return err return err
} }