From 13645b4ee580198748f949f2118201eab15309a8 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 17 Oct 2022 19:45:43 -0300 Subject: [PATCH] stop caching blocks. it was causing 100% CPU usage. --- getblock.go | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/getblock.go b/getblock.go index 6f39030..e3ef62e 100644 --- a/getblock.go +++ b/getblock.go @@ -8,32 +8,16 @@ import ( "fmt" "io/ioutil" "net/http" - "strconv" "strings" - "time" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/rif/cache2go" ) -var ( - heightCache = make(map[int64]string) - blockCache = cache2go.New(2, time.Minute*20) -) - -type cachedBlock struct { - hash string - block string -} +var heightCache = make(map[int64]string) func getBlock(height int64) (block, hash string, err error) { - if res, ok := blockCache.Get(strconv.Itoa(int(height))); ok { - data := res.(cachedBlock) - return data.block, data.hash, nil - } - hash, err = getHash(height) if err != nil { return @@ -102,10 +86,7 @@ func getBlock(height int64) (block, hash string, err error) { } } - delete(heightCache, height) - blockhex := hex.EncodeToString(block) - blockCache.Set(strconv.Itoa(int(height)), cachedBlock{block: blockhex, hash: hash}) return blockhex, hash, nil } @@ -146,8 +127,6 @@ func getHash(height int64) (hash string, err error) { continue } - heightCache[height] = hash - return hash, nil }