github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/baseapp/cache.go (about)

     1  package baseapp
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/tendermint/go-amino"
     8  )
     9  
    10  type blockDataCache struct {
    11  	txLock sync.RWMutex
    12  	txs    map[string]types.Tx
    13  }
    14  
    15  func NewBlockDataCache() *blockDataCache {
    16  	return &blockDataCache{
    17  		txs: make(map[string]types.Tx),
    18  	}
    19  }
    20  
    21  func (cache *blockDataCache) SetTx(txRaw []byte, tx types.Tx) {
    22  	if cache == nil {
    23  		return
    24  	}
    25  	cache.txLock.Lock()
    26  	// txRaw should be immutable, so no need to copy it
    27  	cache.txs[amino.BytesToStr(txRaw)] = tx
    28  	cache.txLock.Unlock()
    29  }
    30  
    31  func (cache *blockDataCache) GetTx(txRaw []byte) (tx types.Tx, ok bool) {
    32  	if cache == nil {
    33  		return
    34  	}
    35  	cache.txLock.RLock()
    36  	tx, ok = cache.txs[amino.BytesToStr(txRaw)]
    37  	cache.txLock.RUnlock()
    38  	return
    39  }
    40  
    41  func (cache *blockDataCache) Clear() {
    42  	if cache == nil {
    43  		return
    44  	}
    45  	cache.txLock.Lock()
    46  	for k := range cache.txs {
    47  		delete(cache.txs, k)
    48  	}
    49  	cache.txLock.Unlock()
    50  }