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 method GetAndAdd to LRU Cache #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vasayxtx
Copy link

Hi,
Sometimes it's useful to get value by key and set a new one atomically (like getset in the Redis).

@hashicorp-cla
Copy link

hashicorp-cla commented Dec 27, 2020

CLA assistant check
All committers have signed the CLA.

@jefferai
Copy link
Member

jefferai commented Jan 4, 2021

This needs to be reworked against the locking/eviction changes that were just merged. But will be happy to have it after!

@vasayxtx
Copy link
Author

vasayxtx commented Jan 5, 2021

@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) {
Copy link
Member

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.

Copy link
Author

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!

Copy link
Member

@jefferai jefferai left a 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.

@vasayxtx
Copy link
Author

vasayxtx commented Jan 5, 2021

One comment -- looks good otherwise. Let me know if you agree with that proposal.

Yes, you are right, fixed.

@vasayxtx
Copy link
Author

@jefferai could you please review and merge if all is ok?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants