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,20 +44,13 @@ func New() *fiber.App {
log.Fatal(err)
}
checkCache, err := core.CheckJokesCache(memory)
err = core.SetAllJSONJoke(db, memory)
if err != nil {
log.Fatalln(err)
}
if !checkCache {
jokes, err := core.GetAllJSONJokes(db)
if err != nil {
log.Fatalln(err)
}
err = memory.Set("jokes", jokes)
if err != nil {
log.Fatalln(err)
}
err = core.SetTotalJoke(db, memory)
if err != nil {
log.Fatalln(err)
}
app.Use(cors.New())

View File

@ -74,6 +74,19 @@ func CheckJokesCache(memory *bigcache.BigCache) (bool, error) {
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.
func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) {
jokes, err := memory.Get("jokes")
@ -99,3 +112,16 @@ func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) {
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
}
jokes, err := core.GetAllJSONJokes(db)
err = core.SetAllJSONJoke(db, memory)
if err != nil {
return err
}
err = memory.Set("jokes", jokes)
err = core.SetTotalJoke(db, memory)
if err != nil {
return err
}

View File

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

View File

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

View File

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