diff --git a/dgraphql/resolvers/trxcache.go b/dgraphql/resolvers/trxcache.go index 94a06a31..41790e5b 100644 --- a/dgraphql/resolvers/trxcache.go +++ b/dgraphql/resolvers/trxcache.go @@ -4,12 +4,14 @@ package resolvers import ( "container/list" + "sync" ) type TrxCache struct { capacity int data map[string]*list.Element order list.List + mu sync.RWMutex } func NewTrxCache(capacity int) *TrxCache { @@ -21,6 +23,9 @@ func NewTrxCache(capacity int) *TrxCache { } func (c *TrxCache) Put(key string) { + c.mu.Lock() + defer c.mu.Unlock() + if ele, exists := c.data[key]; exists { // If key already exists, move it to the front c.order.MoveToFront(ele) @@ -38,6 +43,9 @@ func (c *TrxCache) Put(key string) { } func (c *TrxCache) Exists(key string) bool { + c.mu.RLock() + defer c.mu.RUnlock() + _, exists := c.data[key] return exists }