-
Notifications
You must be signed in to change notification settings - Fork 502
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 method GetAndAdd to LRU Cache #82
base: main
Are you sure you want to change the base?
Conversation
This needs to be reworked against the locking/eviction changes that were just merged. But will be happy to have it after! |
eb1f8f2
to
6b74291
Compare
@jefferai ok, done. Please review. |
@@ -93,6 +93,22 @@ func (c *Cache) Get(key interface{}) (value interface{}, ok bool) { | |||
return value, ok | |||
} | |||
|
|||
// GetAndAdd returns the previous key's value from the cache and adds a new one. | |||
func (c *Cache) GetAndAdd(key, value interface{}) (previous interface{}, ok, evicted bool) { |
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.
Given the changes in simplelru, I think you could do the same here -- instead of copying the function entirely, modify Add above to call GetAndAdd and simply throw away the parameters that aren't a part of the call.
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.
Ah, yes, it's my bad. Refactored, thanks!
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.
One comment -- looks good otherwise. Let me know if you agree with that proposal.
6b74291
to
b1235e0
Compare
Yes, you are right, fixed. |
@jefferai could you please review and merge if all is ok? |
Hi,
Sometimes it's useful to get value by key and set a new one atomically (like getset in the Redis).