Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan-Ultra committed May 29, 2024
1 parent 4152a7c commit 1da974f
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions dgraphql/resolvers/trxcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}
}
}

0 comments on commit 1da974f

Please sign in to comment.