~blainsmith/shardcache

7a20aab625b2855c011b580c4928f8bba2cc8db8 — Blain Smith 27 days ago 9da8c65 main
read lock just the shards
1 files changed, 3 insertions(+), 4 deletions(-)

M cache.go
M cache.go => cache.go +3 -4
@@ 5,13 5,12 @@ import (
)

type shard struct {
	mu    sync.Mutex
	mu    sync.RWMutex
	items map[uint64]interface{}
}

// ShardCache will keep track and shard keys based on the number of shards it holds around mutexes so this is safe for concurrent use
type ShardCache struct {
	mu     sync.RWMutex
	n      uint64
	shards []*shard
}


@@ 36,8 35,8 @@ func New(shards uint64) *ShardCache {

// Get a key from the cache and only use a read lock to access it
func (sc *ShardCache) Get(key uint64) interface{} {
	sc.mu.RLock()
	defer sc.mu.RUnlock()
	sc.shards[key&sc.n].mu.RLock()
	defer sc.shards[key&sc.n].mu.RUnlock()

	return sc.shards[key&sc.n].items[key]
}