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

Added ModifyOrSetFunc #112

Closed
wants to merge 1 commit into from

Conversation

owen5mith
Copy link

Hello,

This PR adds a ModifyOrSetFunc method to the cache. The main motivation for this was to avoid allocations when using the cache to store slices as well as to have an atomic way of creating or updating these slices. ModifyOrSetFunc takes two function arguments, one that is called if a key already exists, and it's return value is used to update the key's value.

The second function is called if the key does not exist, with it's return value being used as the key's value. By using a function for the Set value, over simply passing the value in, such as in GetOrSet, you avoid any allocations if the key is already present, which when being used with large values such as make([]int, 0, 10000), provides a significant speedup as well as reduced GC load.

I'm conscious I don't have a corresponding issue for this, so feel free to decline if this isn't something you think should be part of this library. I did see #72 which feels loosely related.

@swithek
Copy link
Contributor

swithek commented Oct 31, 2023

Though I understand why this might be needed in some cases, I'm not sure whether it should be added to the library. Perhaps, as you said, we should focus on the transaction feature first?

@owen5mith owen5mith closed this Oct 31, 2023
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.

2 participants