Skip to content

Commit

Permalink
refactor: change library from in-memory to be pogreb (suitable for bi…
Browse files Browse the repository at this point in the history
…g data)
  • Loading branch information
TGRZiminiar committed Sep 11, 2024
1 parent 8caf92f commit 4cbafd9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
42 changes: 29 additions & 13 deletions cache-api.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
package main

import (
"encoding/json"
"log/slog"

"github.com/akrylysov/pogreb"
"github.com/gofiber/fiber/v2"
"github.com/patrickmn/go-cache"
)

type (
CacheApi struct {
Cache *cache.Cache
Cache *pogreb.DB
authorizeKey string
}

CacheInterface interface {
HandleCacheApi(c *fiber.Ctx) error
}

Temp struct {
}
)

func newCacheApi(cache *cache.Cache, authorizeKey string) CacheInterface {
// newCacheApi creates a new instance of CacheApi with the given cache and authorize key
func newCacheApi(cache *pogreb.DB, authorizeKey string) CacheInterface {
return &CacheApi{
Cache: cache,
authorizeKey: authorizeKey,
}
}

// HandleCacheApi handles the cache API request. It checks if the cache is available,
// if so, it returns the cached data. If not, it calls the handleCache function to
// retrieve the data, store it in the cache, and return it
func (ca *CacheApi) HandleCacheApi(c *fiber.Ctx) error {

cacheData, exist := ca.Cache.Get(API_CACHE_KEY)
if exist {
if data, ok := cacheData.([]Project); ok {
cacheData, err := ca.Cache.Get([]byte(API_CACHE_KEY))
if err == nil && len(cacheData) > 0 {
var data []Project
if err := json.Unmarshal(cacheData, &data); err == nil {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"msg": "ok",
"data": data,
Expand All @@ -43,7 +45,7 @@ func (ca *CacheApi) HandleCacheApi(c *fiber.Ctx) error {
res, err := handleCache(ca.Cache, ca.authorizeKey)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"msg": err,
"msg": err.Error(),
})
}

Expand All @@ -53,12 +55,26 @@ func (ca *CacheApi) HandleCacheApi(c *fiber.Ctx) error {
})
}

func handleCache(c *cache.Cache, authorizeKey string) ([]Project, error) {
// handleCache retrieves all projects using the given authorize key,
// marshals the result into JSON, stores it in the cache, and returns the result
func handleCache(c *pogreb.DB, authorizeKey string) ([]Project, error) {
res, err := GetAllProjects(authorizeKey)
if err != nil {
slog.Error("failed to handle cache", "error", err)
return []Project{}, err
}
c.Set(API_CACHE_KEY, res, cache.NoExpiration)

jsonData, err := json.Marshal(res)
if err != nil {
slog.Error("failed to marshal projects", "error", err)
return []Project{}, err
}

err = c.Put([]byte(API_CACHE_KEY), jsonData)
if err != nil {
slog.Error("failed to store in cache", "error", err)
return []Project{}, err
}

return res, nil
}
3 changes: 2 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main

var (
API_CACHE_KEY string = "cache"
API_CACHE_KEY string = "agora-cache"
API_FILE_FOLDER string = "agora-data"
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/catalyzt-tech/RetroPGFHub-API-Cache
go 1.22.2

require (
github.com/akrylysov/pogreb v0.10.2
github.com/gofiber/fiber/v2 v2.52.5
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/robfig/cron/v3 v3.0.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/akrylysov/pogreb v0.10.2 h1:e6PxmeyEhWyi2AKOBIJzAEi4HkiC+lKyCocRGlnDi78=
github.com/akrylysov/pogreb v0.10.2/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
Expand All @@ -13,8 +15,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"syscall"
"time"

"github.com/akrylysov/pogreb"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/patrickmn/go-cache"
"github.com/robfig/cron/v3"
)

type Server struct {
app *fiber.App
url string
cache *cache.Cache
cache *pogreb.DB
cron *cron.Cron
authorizeKey string
corsOrigin string
Expand All @@ -27,9 +27,11 @@ type Server struct {
func main() {

var authorKey string = os.Getenv("AUTHORIZE_KEY")
fmt.Println(authorKey)
if authorKey == "" {
panic("Can't find AUTHORIZE_KEY in environment")
}

var port string = os.Getenv("PORT")
if port == "" {
panic("Can't find PORT in environment")
Expand All @@ -41,6 +43,11 @@ func main() {
panic("Can't find corsOrigin in environment")
}

cache, err := pogreb.Open(API_FILE_FOLDER, nil)
if err != nil {
panic(err)
}

var s *Server = &Server{
app: fiber.New(fiber.Config{
BodyLimit: 4 * 1024 * 1024,
Expand All @@ -51,7 +58,7 @@ func main() {
},
}),
url: port,
cache: cache.New(cache.NoExpiration, cache.NoExpiration),
cache: cache,
cron: cron.New(
cron.WithParser(
cron.NewParser(
Expand All @@ -64,7 +71,7 @@ func main() {
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

fmt.Println("Saving data to cache . . .")
_, err := handleCache(s.cache, s.authorizeKey)
_, err = handleCache(s.cache, s.authorizeKey)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 4cbafd9

Please sign in to comment.