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

     1  package flatkv
     2  
     3  import "sync"
     4  
     5  type Cache struct {
     6  	mtx  sync.RWMutex
     7  	data map[string][]byte
     8  }
     9  
    10  func newCache() *Cache {
    11  	return &Cache{
    12  		data: make(map[string][]byte),
    13  	}
    14  }
    15  
    16  func (c *Cache) get(key []byte) (value []byte, ok bool) {
    17  	strKey := string(key)
    18  	c.mtx.RLock()
    19  	defer c.mtx.RUnlock()
    20  	value, ok = c.data[strKey]
    21  	return
    22  }
    23  
    24  func (c *Cache) add(key, value []byte) {
    25  	strKey := string(key)
    26  	c.mtx.Lock()
    27  	defer c.mtx.Unlock()
    28  	c.data[strKey] = value
    29  }
    30  
    31  func (c *Cache) delete(key []byte) {
    32  	strKey := string(key)
    33  	c.mtx.Lock()
    34  	defer c.mtx.Unlock()
    35  	delete(c.data, strKey)
    36  }
    37  
    38  func (c *Cache) copy() map[string][]byte {
    39  	c.mtx.RLock()
    40  	defer c.mtx.RUnlock()
    41  	copyMap := make(map[string][]byte, len(c.data))
    42  	for k, v := range c.data {
    43  		copyMap[k] = v
    44  	}
    45  	return copyMap
    46  }
    47  
    48  func (c *Cache) reset() {
    49  	c.mtx.Lock()
    50  	defer c.mtx.Unlock()
    51  	c.data = make(map[string][]byte)
    52  }