From 87e1c929a90b3a85513934816aec49ff39769bbf Mon Sep 17 00:00:00 2001 From: Lenart Kos <39205323+Lenart12@users.noreply.github.com> Date: Wed, 5 Feb 2025 09:10:25 +0100 Subject: [PATCH] Plugins: force http cache by setting cache headers (#18601) --- plugin/http.go | 56 +++++++++++++------------ templates/definition/meter/enphase.yaml | 11 +++++ 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/plugin/http.go b/plugin/http.go index d2b9029b7..917fa4b6e 100644 --- a/plugin/http.go +++ b/plugin/http.go @@ -23,11 +23,7 @@ type HTTP struct { url, method string headers map[string]string body string - cache time.Duration - updated time.Time pipeline *pipeline.Pipeline - val []byte // Cached http response value - err error // Cached http response error } func init() { @@ -99,7 +95,6 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur Helper: request.NewHelper(log), url: uri, method: method, - cache: cache, } // http cache @@ -108,6 +103,16 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur Transport: p.Client.Transport, } + if cache > 0 { + cacheHeader := fmt.Sprintf("max-age=%d, must-revalidate", int(cache.Seconds())) + p.Client.Transport = &transport.Decorator{ + Decorator: transport.DecorateHeaders(map[string]string{ + "Cache-Control": cacheHeader, + }), + Base: p.Client.Transport, + } + } + // ignore the self signed certificate if insecure { p.Client.Transport = request.NewTripper(log, transport.Insecure()) @@ -152,30 +157,27 @@ func (p *HTTP) WithAuth(typ, user, password string) (*HTTP, error) { // request executes the configured request or returns the cached value func (p *HTTP) request(url string, body string) ([]byte, error) { - if time.Since(p.updated) >= p.cache { - var b io.Reader - if p.method != http.MethodGet { - b = strings.NewReader(body) - } - - url := util.DefaultScheme(url, "http") - - // empty method becomes GET - req, err := request.New(p.method, url, b, p.headers) - if err != nil { - return []byte{}, err - } - - p.val, p.err = p.DoBody(req) - if p.err != nil { - if err := knownErrors(p.val); err != nil { - p.err = err - } - } - p.updated = time.Now() + var b io.Reader + if p.method != http.MethodGet { + b = strings.NewReader(body) } - return p.val, p.err + url = util.DefaultScheme(url, "http") + + // empty method becomes GET + req, err := request.New(p.method, url, b, p.headers) + if err != nil { + return []byte{}, err + } + + val, err := p.DoBody(req) + if err != nil { + if err2 := knownErrors(val); err2 != nil { + err = err2 + } + } + + return val, err } var _ Getters = (*HTTP)(nil) diff --git a/templates/definition/meter/enphase.yaml b/templates/definition/meter/enphase.yaml index 300e0e6b2..59f310f85 100644 --- a/templates/definition/meter/enphase.yaml +++ b/templates/definition/meter/enphase.yaml @@ -29,6 +29,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: .consumption[] | select(.measurementType == "net-consumption").wNow currents: - source: http @@ -39,6 +40,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (( .consumption[] | select(.measurementType == "net-consumption").activeCount >= 1 ) and ( .consumption[] | select(.measurementType == "net-consumption").lines | length >= 1 )) then .consumption[] | select(.measurementType == "net-consumption").lines[0].rmsCurrent else 0 end - source: http uri: http://{{ .host }}/production.json?details=1 @@ -48,6 +50,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (( .consumption[] | select(.measurementType == "net-consumption").activeCount >= 1 ) and ( .consumption[] | select(.measurementType == "net-consumption").lines | length >= 2 )) then .consumption[] | select(.measurementType == "net-consumption").lines[1].rmsCurrent else 0 end - source: http uri: http://{{ .host }}/production.json?details=1 @@ -57,6 +60,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (( .consumption[] | select(.measurementType == "net-consumption").activeCount >= 1 ) and ( .consumption[] | select(.measurementType == "net-consumption").lines | length >= 3 )) then .consumption[] | select(.measurementType == "net-consumption").lines[2].rmsCurrent else 0 end {{- end }} {{- if eq .usage "pv" }} @@ -69,6 +73,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (.production | length) > 1 and (.production[] | select(.measurementType == "production").activeCount >= 1) then .production[] | select(.measurementType == "production").wNow else .production[] | select(.type == "inverters").wNow end energy: source: http @@ -79,6 +84,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (.production | length) > 1 and (.production[] | select(.measurementType == "production").activeCount >= 1) then .production[] | select(.measurementType == "production").whLifetime else .production[] | select(.type == "inverters").whLifetime end scale: 0.001 currents: @@ -90,6 +96,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (( .production[] | select(.measurementType == "production").activeCount >= 1 ) and ( .production[] | select(.measurementType == "production").lines | length >= 1 )) then .production[] | select(.measurementType == "production").lines[0].rmsCurrent else 0 end - source: http uri: http://{{ .host }}/production.json?details=1 @@ -99,6 +106,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (( .production[] | select(.measurementType == "production").activeCount >= 1 ) and ( .production[] | select(.measurementType == "production").lines | length >= 2 )) then .production[] | select(.measurementType == "production").lines[1].rmsCurrent else 0 end - source: http uri: http://{{ .host }}/production.json?details=1 @@ -108,6 +116,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: if (( .production[] | select(.measurementType == "production").activeCount >= 1 ) and ( .production[] | select(.measurementType == "production").lines | length >= 3 )) then .production[] | select(.measurementType == "production").lines[2].rmsCurrent else 0 end {{- end }} {{- if eq .usage "battery" }} @@ -120,6 +129,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: .storage[] | .wNow soc: source: http @@ -130,6 +140,7 @@ render: | password: {{ .token }} insecure: true {{- end }} + cache: 5s jq: '[.[].devices[] | select(.percentFull != null) | .percentFull] | add / length' capacity: {{ .capacity }} # kWh {{- end }}