Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan-Ultra committed May 29, 2024
1 parent 21029cc commit 11db5ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dgraphql/resolvers/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func processMatchOrError(ctx context.Context, m *matchOrError, rows [][]*pbcodec
}, nil
}
//Saved proccessed transaction
processedTrxCache.Put(out.trxTrace.GetId(), true)
processedTrxCache.Put(out.trxTrace.GetId())
}

return out, nil
Expand Down Expand Up @@ -484,7 +484,7 @@ func (r *Root) streamSearchTracesBoth(forward bool, ctx context.Context, args St
}

//ultra-duncan --- BLOCK-2245 prevent duplication when query --- Initialize how many cache should be keep track
processedTrxCache := NewTrxCache(1000000)
processedTrxCache := NewTrxCache(100000)

//////////////////////////////////////////////////////////////////////
// Billable event on GraphQL Subscriptions
Expand Down
17 changes: 6 additions & 11 deletions dgraphql/resolvers/trxcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ type TrxCache struct {
order *list.List
}

type TrxEntry struct {
key string
value interface{}
}

func NewTrxCache(capacity int) *TrxCache {
return &TrxCache{
capacity: capacity,
Expand All @@ -25,10 +20,10 @@ func NewTrxCache(capacity int) *TrxCache {
}
}

func (c *TrxCache) Put(key string, value interface{}) {
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] = value
c.data[key] = true
c.moveToFront(key)
return
}
Expand All @@ -39,8 +34,8 @@ func (c *TrxCache) Put(key string, value interface{}) {
}

// Add new entry to the map and the front of the list
c.data[key] = value
c.order.PushFront(TrxEntry{key, value})
c.data[key] = true
c.order.PushFront(key)
}

func (c *TrxCache) Get(key string) (interface{}, bool) {
Expand All @@ -62,14 +57,14 @@ func (c *TrxCache) evictOldest() {
oldest := c.order.Back()
if oldest != nil {
c.order.Remove(oldest)
delete(c.data, oldest.Value.(TrxEntry).key)
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.(TrxEntry).key == key {
if e.Value.(string) == key {
c.order.MoveToFront(e)
break
}
Expand Down

0 comments on commit 11db5ce

Please sign in to comment.