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