From e998809d102af40cd99781603da77573a503655f Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 13 Sep 2025 15:20:36 +0200 Subject: [PATCH] Tariffs: avoid duplicate cache writes --- tariff/proxy.go | 11 ++++++++++- tariff/proxy_cache.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tariff/proxy.go b/tariff/proxy.go index 2caba2b46..b8bbfd0bd 100644 --- a/tariff/proxy.go +++ b/tariff/proxy.go @@ -2,7 +2,9 @@ package tariff import ( "context" + "crypto/sha256" "errors" + "fmt" "slices" "sync" "time" @@ -14,7 +16,8 @@ import ( // CachingProxy wraps a tariff with caching type CachingProxy struct { - mu sync.Mutex + mu sync.Mutex + hash [32]byte key string ctx context.Context @@ -165,6 +168,12 @@ func (p *CachingProxy) cacheGet(until time.Time) (*cached, error) { } func (p *CachingProxy) cachePut(typ api.TariffType, rates api.Rates) error { + hash := sha256.Sum256(fmt.Append(nil, rates)) + if hash == p.hash { + return nil + } + + p.hash = hash return cachePut(p.key, typ, rates) } diff --git a/tariff/proxy_cache.go b/tariff/proxy_cache.go index e01672fdc..b4fe00ed0 100644 --- a/tariff/proxy_cache.go +++ b/tariff/proxy_cache.go @@ -14,7 +14,7 @@ type cached struct { } func cacheKey(typ string, other map[string]any) string { - return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s-%v", typ, other)))) + return fmt.Sprintf("%x", sha256.Sum256(fmt.Appendf(nil, "%s-%v", typ, other))) } func cachePut(key string, typ api.TariffType, rates api.Rates) error {