From 0167ea735935bfaec7881337b99f420a5a935e58 Mon Sep 17 00:00:00 2001 From: John Bakhmat Date: Wed, 5 Jun 2024 09:48:12 +0300 Subject: [PATCH] caching i guess --- .gitignore | 1 + go.mod | 1 + go.sum | 2 ++ graphql/graphql.go | 12 ++++++------ main.go | 24 +++++++++++++++++------- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 5ba2e9c..3ec016a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env +.envrc /bin diff --git a/go.mod b/go.mod index bd5b8f3..0be2a4a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/gofiber/fiber/v3 v3.0.0-20240113152607-1588b6b60210 + github.com/patrickmn/go-cache v2.1.0+incompatible github.com/shurcooL/githubv4 v0.0.0-20231126234147-1cffa1f02456 golang.org/x/oauth2 v0.16.0 ) diff --git a/go.sum b/go.sum index 0e4319a..755a3bd 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shurcooL/githubv4 v0.0.0-20231126234147-1cffa1f02456 h1:6dExqsYngGEiixqa1vmtlUd+zbyISilg0Cf3GWVdeYM= diff --git a/graphql/graphql.go b/graphql/graphql.go index d5a156d..8920685 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -10,12 +10,12 @@ import ( ) type Project struct { - Name string - Description string - Url string - Stars int - Forks int - Languages []string + Name string `json:"name"` + Description string `json:"description"` + Url string `json:"url"` + Stars int `json:"stars"` + Forks int `json:"forks"` + Languages []string `json:"languages"` } func GetProjects(username string) ([]Project, error) { diff --git a/main.go b/main.go index d161f2f..7eb36d0 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,11 @@ import ( "log" "os" "strconv" + "time" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/cors" + "github.com/patrickmn/go-cache" "johnbakhmat.tech/pinned/graphql" ) @@ -22,22 +24,30 @@ func main() { log.Fatal(err) } - app := fiber.New() - - app.Use(cors.New()) + c := cache.New(5*time.Minute, 10*time.Minute) + app := fiber.New() + app.Use(cors.New()) app.Get("/", func(c fiber.Ctx) error { return c.SendStatus(200) }) - app.Get("/projects/:username", func(c fiber.Ctx) error { - username := c.Params("username", "johnbakhmat") + app.Get("/projects/:username", func(ctx fiber.Ctx) error { + username := ctx.Params("username", "johnbakhmat") + + if projects_cache, cache_hit := c.Get(username); cache_hit { + log.Println("Cache hit") + projects := projects_cache.(*[]graphql.Project) + return ctx.JSON(*projects) + } + projects, err := graphql.GetProjects(username) if err != nil { log.Println(err) - return c.SendStatus(500) + return ctx.SendStatus(500) } log.Printf("%v", projects) - return c.JSON(projects) + c.Set(username, &projects, cache.DefaultExpiration) + return ctx.JSON(projects) }) log.Fatal(app.Listen(fmt.Sprintf(":%d", port)))