mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-22 03:30:35 +00:00
add changes (#12697)
Co-authored-by: terencechain <terence@prysmaticlabs.com>
This commit is contained in:
parent
a664a07303
commit
9a4670ec64
23
cache/nonblocking/lru.go
vendored
23
cache/nonblocking/lru.go
vendored
@ -18,6 +18,7 @@ type LRU[K comparable, V any] struct {
|
||||
evictList *lruList[K, V]
|
||||
items map[K]*entry[K, V]
|
||||
onEvict EvictCallback[K, V]
|
||||
getChan chan *entry[K, V]
|
||||
}
|
||||
|
||||
// NewLRU constructs an LRU of the given size
|
||||
@ -25,13 +26,19 @@ func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V]) (*LRU[K,
|
||||
if size <= 0 {
|
||||
return nil, errors.New("must provide a positive size")
|
||||
}
|
||||
// Initialize the channel buffer size as being 10% of the cache size.
|
||||
chanSize := size / 10
|
||||
|
||||
c := &LRU[K, V]{
|
||||
size: size,
|
||||
evictList: newList[K, V](),
|
||||
items: make(map[K]*entry[K, V]),
|
||||
onEvict: onEvict,
|
||||
getChan: make(chan *entry[K, V], chanSize),
|
||||
}
|
||||
// Spin off separate go-routine to handle evict list
|
||||
// operations.
|
||||
go c.handleGetRequests()
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@ -77,12 +84,7 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
|
||||
c.itemsLock.RUnlock()
|
||||
|
||||
// Make this get function non-blocking for multiple readers.
|
||||
go func() {
|
||||
c.evictListLock.Lock()
|
||||
c.evictList.moveToFront(ent)
|
||||
c.evictListLock.Unlock()
|
||||
}()
|
||||
|
||||
c.getChan <- ent
|
||||
return ent.value, true
|
||||
}
|
||||
c.itemsLock.RUnlock()
|
||||
@ -133,3 +135,12 @@ func (c *LRU[K, V]) removeElement(e *entry[K, V]) {
|
||||
c.onEvict(e.key, e.value)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *LRU[K, V]) handleGetRequests() {
|
||||
for {
|
||||
entry := <-c.getChan
|
||||
c.evictListLock.Lock()
|
||||
c.evictList.moveToFront(entry)
|
||||
c.evictListLock.Unlock()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user