jokes-bapak2/api/app/v1/core/joke_setter.go

54 lines
950 B
Go
Raw Normal View History

2021-07-19 10:21:08 +00:00
package core
import (
"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 {
2021-07-19 18:35:30 +00:00
err = SetAllJSONJoke(db, memory)
if err != nil {
return err
}
2021-07-19 10:21:08 +00:00
}
jokes, err := memory.Get("jokes")
if err != nil {
return err
}
var data []Joke
2021-07-19 10:21:08 +00:00
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
}