From 6febafdf9d87fa36e65adc61cb552c5242b9d52a Mon Sep 17 00:00:00 2001 From: Stefan Kostic Date: Thu, 10 Mar 2022 23:38:51 +0100 Subject: [PATCH] Add and use cache client --- main.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 32235ba..ce94d67 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "os/signal" "time" + cache "github.com/SporkHubr/echo-http-cache" + "github.com/SporkHubr/echo-http-cache/adapter/memory" "github.com/getAlby/lndhub.go/controllers" "github.com/getAlby/lndhub.go/db" "github.com/getAlby/lndhub.go/db/migrations" @@ -153,7 +155,7 @@ func main() { //Index page endpoints, no Authorization required homeController := controllers.NewHomeController(svc, indexHtml) - e.GET("/", homeController.Home) + e.GET("/", homeController.Home, createCacheClient().Middleware()) e.GET("/qr", homeController.QR) //workaround, just adding /static would make a request to these resources hit the authorized group e.GET("/static/css/*", echo.WrapHandler(http.FileServer(http.FS(staticContent)))) @@ -180,3 +182,25 @@ func main() { e.Logger.Fatal(err) } } + +func createCacheClient() *cache.Client { + memcached, err := memory.NewAdapter( + memory.AdapterWithAlgorithm(memory.LRU), + memory.AdapterWithCapacity(10000000), + ) + + if err != nil { + log.Fatalf("Error creating cache client memory adapter: %v", err) + } + + cacheClient, err := cache.NewClient( + cache.ClientWithAdapter(memcached), + cache.ClientWithTTL(10*time.Minute), + cache.ClientWithRefreshKey("opn"), + ) + + if err != nil { + log.Fatalf("Error creating cache client: %v", err) + } + return cacheClient +}