diff --git a/dgraphql/resolvers/trxcache.go b/dgraphql/resolvers/trxcache.go index 1505629c..1255d873 100644 --- a/dgraphql/resolvers/trxcache.go +++ b/dgraphql/resolvers/trxcache.go @@ -8,23 +8,22 @@ import ( type TrxCache struct { capacity int - data map[string]bool + data map[string]*list.Element order *list.List } func NewTrxCache(capacity int) *TrxCache { return &TrxCache{ capacity: capacity, - data: make(map[string]bool), + data: make(map[string]*list.Element), order: list.New(), } } func (c *TrxCache) Put(key string) { - if _, exists := c.data[key]; exists { - // If key already exists, update value and move it to the front - c.data[key] = true - c.moveToFront(key) + if ele, exists := c.data[key]; exists { + // If key already exists, move it to the front + c.order.MoveToFront(ele) return } @@ -33,18 +32,9 @@ func (c *TrxCache) Put(key string) { c.evictOldest() } - // Add new entry to the map and the front of the list - c.data[key] = true - c.order.PushFront(key) -} - -func (c *TrxCache) Get(key string) (interface{}, bool) { - if value, exists := c.data[key]; exists { - // Move accessed item to the front - c.moveToFront(key) - return value, true - } - return nil, false + // Add new entry to the front of the list and to the map + ele := c.order.PushFront(key) + c.data[key] = ele } func (c *TrxCache) Exists(key string) bool { @@ -60,13 +50,3 @@ func (c *TrxCache) evictOldest() { delete(c.data, oldest.Value.(string)) } } - -func (c *TrxCache) moveToFront(key string) { - // Move accessed or updated key to the front of the list - for e := c.order.Front(); e != nil; e = e.Next() { - if e.Value.(string) == key { - c.order.MoveToFront(e) - break - } - } -}