Skip to content

Commit

Permalink
stop caching blocks. it was causing 100% CPU usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 18, 2022
1 parent adf8e3e commit 13645b4
Showing 1 changed file with 1 addition and 22 deletions.
23 changes: 1 addition & 22 deletions getblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -146,8 +127,6 @@ func getHash(height int64) (hash string, err error) {
continue
}

heightCache[height] = hash

return hash, nil
}

Expand Down

0 comments on commit 13645b4

Please sign in to comment.