github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/storages/account_cache.go (about)

     1  package storages
     2  
     3  import (
     4  	"github.com/hashicorp/golang-lru"
     5  	"github.com/sixexorg/magnetic-ring/common"
     6  	"github.com/sixexorg/magnetic-ring/store/mainchain/states"
     7  )
     8  
     9  const (
    10  	ACCOUNT_CACHE_SIZE = 100000
    11  )
    12  
    13  type AccountCache struct {
    14  	accountCache *lru.ARCCache
    15  }
    16  
    17  func NewAccountCache() (*AccountCache, error) {
    18  	accountCache, err := lru.NewARC(ACCOUNT_CACHE_SIZE)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	return &AccountCache{
    23  		accountCache: accountCache,
    24  	}, nil
    25  }
    26  
    27  func (this *AccountCache) GetState(address common.Address) *states.AccountState {
    28  	state, ok := this.accountCache.Get(address.ToString())
    29  	if !ok {
    30  		return nil
    31  	}
    32  	return state.(*states.AccountState)
    33  }
    34  
    35  func (this *AccountCache) AddState(state *states.AccountState) {
    36  	this.accountCache.Add(state.Address.ToString(), state)
    37  }
    38  
    39  func (this *AccountCache) DeleteState(address common.Address) {
    40  	this.accountCache.Remove(address.ToString())
    41  }