-
Notifications
You must be signed in to change notification settings - Fork 201
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
Add a helper method to set eviction function after construction #318
Conversation
@@ -44,6 +44,11 @@ func NewWithEvictionFunc(size int, f EvictionFunc) *Cache { | |||
return c | |||
} | |||
|
|||
// SetEvictionFunc updates the eviction func | |||
func (c *Cache) SetEvictionFunc(f EvictionFunc) { | |||
c.cache.OnEvicted = f |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
It seems correct, but it will be nice if you can just add an unit test, looking at this https://github.com/kubernetes/utils/pull/275/files it seems you can just copy paste that but instead of using the constructor you use this new helper |
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
8c2389e
to
7edc6f1
Compare
indeed! i tightened the api a bit, by allowing the set function only once as well. added tests. |
/lgtm Thanks |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: aojea, dims The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
We have a use case in kubernetes/kubernetes#128507 where we set
OnEvicted
later.https://github.com/dims/utils/pull/new/add-a-helper-method-to-set-eviction-function-after-construction
What type of PR is this?
/kind feature
What this PR does / why we need it:
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Release note: