github.com/saintwish/kv@v1.0.4/kvmap/shard.go (about)

     1  package kvmap
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  //used internally
     8  type shard[K comparable, V any] struct {
     9  	Map map[K]V
    10  	*sync.RWMutex //mutex
    11  }
    12  
    13  func newShard[K comparable, V any]() shard[K, V] {
    14  	return shard[K, V] {
    15  		Map: make(map[K]V, 0),
    16  		RWMutex: &sync.RWMutex{},
    17  	}
    18  }
    19  
    20  func (m shard[K, V]) set(key K, val V) {
    21  	m.Lock()
    22  
    23  	m.Map[key] = val
    24  
    25  	m.Unlock()
    26  }
    27  
    28  func (m shard[K, V]) get(key K) (val V) {
    29  	m.RLock()
    30  
    31  	val = m.Map[key]
    32  
    33  	m.RUnlock()
    34  
    35  	return
    36  }