Skip to content

Commit

Permalink
add RangeEvictOnError() (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Oct 9, 2021
1 parent be5ee19 commit dacb910
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.14
go-version: ^1.17

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand Down
37 changes: 25 additions & 12 deletions cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func New(maxEntries int) *Cache {

// Add adds a value to the cache.
func (c *Cache) Add(key Key, value interface{}) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.cache == nil {
c.cache = make(map[interface{}]*list.Element)
c.ll = list.New()
}
c.mutex.Lock()
defer c.mutex.Unlock()
if ee, ok := c.cache[key]; ok {
c.ll.MoveToFront(ee)
ee.Value.(*entry).value = value
Expand All @@ -83,11 +83,11 @@ func (c *Cache) Add(key Key, value interface{}) {

// Get looks up a key's value from the cache.
func (c *Cache) Get(key Key) (value interface{}, ok bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.cache == nil {
return
}
c.mutex.Lock()
defer c.mutex.Unlock()
if ele, hit := c.cache[key]; hit {
c.ll.MoveToFront(ele)
return ele.Value.(*entry).value, true
Expand All @@ -97,27 +97,27 @@ func (c *Cache) Get(key Key) (value interface{}, ok bool) {

// Remove removes the provided key from the cache.
func (c *Cache) Remove(key Key) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.cache == nil {
return
}
c.mutex.Lock()
if ele, hit := c.cache[key]; hit {
c.removeElement(ele)
}
c.mutex.Unlock()
}

// RemoveOldest removes the oldest item from the cache.
func (c *Cache) RemoveOldest() {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.cache == nil {
return
}
c.mutex.Lock()
ele := c.ll.Back()
if ele != nil {
c.removeElement(ele)
}
c.mutex.Unlock()
}

func (c *Cache) removeElement(e *list.Element) {
Expand All @@ -131,13 +131,12 @@ func (c *Cache) removeElement(e *list.Element) {

// Len returns the number of items in the cache.
func (c *Cache) Len() int {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.cache == nil {
return 0
}
c.mutex.RLock()
length := c.ll.Len()
c.mutex.RUnlock()
return length
return c.ll.Len()
}

// Clear purges all stored items from the cache.
Expand All @@ -156,10 +155,24 @@ func (c *Cache) Clear() {

// Range call f on every key
func (c *Cache) Range(f func(key Key, value interface{}) bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
for _, e := range c.cache {
kv := e.Value.(*entry)
if !f(kv.key, kv.value) {
break
}
}
}

// RangeEvictOnError removes the key if call failed
func (c *Cache) RangeEvictOnError(f func(key Key, value interface{}) error) {
c.mutex.Lock()
defer c.mutex.Unlock()
for _, e := range c.cache {
kv := e.Value.(*entry)
if f(kv.key, kv.value) != nil {
c.removeElement(e)
}
}
}
25 changes: 15 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
module github.com/iotexproject/go-pkgs

go 1.14
go 1.17

require (
github.com/aristanetworks/goarista v0.0.0-20190429220743-799535f6f364 // indirect
github.com/btcsuite/btcd v0.0.0-20190427004231-96897255fd17 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/cespare/cp v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/dustinxie/gmsm v1.2.1-0.20200206225615-ad1978e2c91f
github.com/ethereum/go-ethereum v1.8.27
github.com/iotexproject/iotex-address v0.2.4
github.com/pborman/uuid v1.2.0 // indirect
github.com/dustinxie/gmsm v1.4.0
github.com/ethereum/go-ethereum v1.10.4
github.com/go-stack/stack v1.8.0 // indirect
github.com/google/uuid v1.1.5 // indirect
github.com/iotexproject/iotex-address v0.2.5
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rjeczalik/notify v0.9.2 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20200822124328-c89045814202
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

replace github.com/ethereum/go-ethereum v1.8.27 => github.com/iotexproject/go-ethereum v1.7.4-0.20210604055808-21e763469056
replace github.com/ethereum/go-ethereum v1.10.4 => github.com/iotexproject/go-ethereum v0.4.0
Loading

0 comments on commit dacb910

Please sign in to comment.