Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper method to set eviction function after construction #318

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package lru

import (
"fmt"
"sync"

groupcache "k8s.io/utils/internal/third_party/forked/golang/golang-lru"
Expand Down Expand Up @@ -44,6 +45,15 @@ func NewWithEvictionFunc(size int, f EvictionFunc) *Cache {
return c
}

// SetEvictionFunc updates the eviction func
func (c *Cache) SetEvictionFunc(f EvictionFunc) error {
if c.cache.OnEvicted != nil {
return fmt.Errorf("lru cache eviction function is already set")
dims marked this conversation as resolved.
Show resolved Hide resolved
}
c.cache.OnEvicted = f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be thread safe and lock before setting the value?

same for NewWithEvictionFunc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point! added it here. But we don't need it in NewWithEvictionFunc as we do not return the internal Cache we create for the user to use until the method exits, so setting c.cache.OnEvicted = f without the locks are fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point! added it here

was that added in a follow-up? not seeing it here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liggitt would you recommend both the lock/unlock in addition to the "return error if it is already set"? i can cut another PR if so

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liggitt would you recommend both the lock/unlock in addition to the "return error if it is already set"?

yeah... going from nil to set still requires a lock since the function can be read/used concurrently from other methods

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and disallowing resetting it once set still sgtm)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up is here #319

return nil
}

// Add adds a value to the cache.
func (c *Cache) Add(key Key, value interface{}) {
c.lock.Lock()
Expand Down
28 changes: 28 additions & 0 deletions lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,31 @@ func TestEviction(t *testing.T) {
t.Errorf("unexpected eviction data: key=%v val=%v", seenKey, seenVal)
}
}

func TestSetEviction(t *testing.T) {
var seenKey Key
var seenVal interface{}

lru := New(1)

err := lru.SetEvictionFunc(func(key Key, value interface{}) {
seenKey = key
seenVal = value
})

if err != nil {
t.Errorf("unexpected error setting eviction function: %v", err)
}

lru.Add(1, 2)
lru.Add(3, 4)

if seenKey != 1 || seenVal != 2 {
t.Errorf("unexpected eviction data: key=%v val=%v", seenKey, seenVal)
}

err = lru.SetEvictionFunc(func(key Key, value interface{}) {})
if err == nil {
t.Errorf("expected error but got none")
}
}
Loading