From a467a794755a32a86c5164579ede818018a3bcd9 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 7 Oct 2023 16:52:25 +0200 Subject: [PATCH] Allow http plugin to cache responses (#10218) --- go.mod | 1 + go.sum | 2 ++ provider/http.go | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/go.mod b/go.mod index 7c9c8bf4d..cd9d1a465 100644 --- a/go.mod +++ b/go.mod @@ -43,6 +43,7 @@ require ( github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 github.com/gregdel/pushover v1.3.0 + github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 github.com/grid-x/modbus v0.0.0-20230713135356-d9fefd3ae5a5 github.com/hashicorp/go-version v1.6.0 github.com/hasura/go-graphql-client v0.10.0 diff --git a/go.sum b/go.sum index 14707e847..1b1dc724c 100644 --- a/go.sum +++ b/go.sum @@ -397,6 +397,8 @@ github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eL github.com/graph-gophers/graphql-transport-ws v0.0.2/go.mod h1:5BVKvFzOd2BalVIBFfnfmHjpJi/MZ5rOj8G55mXvZ8g= github.com/gregdel/pushover v1.3.0 h1:CewbxqsThoN/1imgwkDKFkRkltaQMoyBV0K9IquQLtw= github.com/gregdel/pushover v1.3.0/go.mod h1:EcaO66Nn1StkpEm1iKtBTV3d2A16SoMsVER1PthX7to= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grid-x/serial v0.0.0-20191104121038-e24bc9bf6f08/go.mod h1:kdOd86/VGFWRrtkNwf1MPk0u1gIjc4Y7R2j7nhwc7Rk= github.com/grid-x/serial v0.0.0-20211107191517-583c7356b3aa h1:Rsn6ARgNkXrsXJIzhkE4vQr5Gbx2LvtEMv4BJOK4LyU= github.com/grid-x/serial v0.0.0-20211107191517-583c7356b3aa/go.mod h1:kdOd86/VGFWRrtkNwf1MPk0u1gIjc4Y7R2j7nhwc7Rk= diff --git a/provider/http.go b/provider/http.go index 6424c3d9d..6c29cdb55 100644 --- a/provider/http.go +++ b/provider/http.go @@ -12,6 +12,7 @@ import ( "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/request" "github.com/evcc-io/evcc/util/transport" + "github.com/gregjones/httpcache" "github.com/jpfielding/go-http-digest/pkg/digest" ) @@ -103,6 +104,11 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, scale float64, cache: cache, } + // http cache + cacheTransport := httpcache.NewMemoryCacheTransport() + cacheTransport.Transport = p.Client.Transport + p.Client.Transport = cacheTransport + // ignore the self signed certificate if insecure { p.Client.Transport = request.NewTripper(log, transport.Insecure())