github.com/turingchain2020/turingchain@v1.1.21/system/mempool/lasttx.go (about) 1 package mempool 2 3 import ( 4 "github.com/turingchain2020/turingchain/common/listmap" 5 "github.com/turingchain2020/turingchain/types" 6 ) 7 8 //LastTxCache 最后放入cache的交易 9 type LastTxCache struct { 10 max int 11 l *listmap.ListMap 12 } 13 14 //NewLastTxCache 创建最后交易的cache 15 func NewLastTxCache(size int) *LastTxCache { 16 return &LastTxCache{ 17 max: size, 18 l: listmap.New(), 19 } 20 } 21 22 //GetLatestTx 返回最新十条加入到txCache的交易 23 func (cache *LastTxCache) GetLatestTx() (txs []*types.Transaction) { 24 cache.l.Walk(func(v interface{}) bool { 25 txs = append(txs, v.(*types.Transaction)) 26 return true 27 }) 28 return txs 29 } 30 31 //Remove remove tx of last cache 32 func (cache *LastTxCache) Remove(tx *types.Transaction) { 33 cache.l.Remove(string(tx.Hash())) 34 } 35 36 //Push tx into LastTxCache 37 func (cache *LastTxCache) Push(tx *types.Transaction) { 38 if cache.l.Size() >= cache.max { 39 v := cache.l.GetTop() 40 if v != nil { 41 cache.Remove(v.(*types.Transaction)) 42 } 43 } 44 cache.l.Push(string(tx.Hash()), tx) 45 }