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

feat: implement SetIfPresent #12

Merged
merged 5 commits into from
Oct 18, 2024
Merged

Conversation

arthhhhh23
Copy link
Contributor

Added to all implementations and most tests

SetIfPresent sets the provided key to the provided value if it already exists in the cache implementation. For MapTTLCache, it also reinitializes the TTL

@egregors
Copy link
Collaborator

Could you please provide some example, why would you want to do that? And as far as you extend public iface, it probably should be represented in readme

Copy link
Owner

@C-Pro C-Pro left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks.

@arthhhhh23
Copy link
Contributor Author

Could you please provide some example, why would you want to do that? And as far as you extend public iface, it probably should be represented in readme

Updated the readme!
In terms of examples, one could use it whenever an atomic get-and-then-set is required. Let's say I'm storing a mapping between (user id, user info) where I have 2 processes, one that updates the user info and one that deletes the key if the user logs out. I might do:

userInfo := m.Get(userId)
userInfo.Nickname = updatedNickname
// some other process deletes the userId key
m.SetIfPresent(userId, userInfo)

without SetIfPresent, we could re-insert the user info even though it's supposed to have been deleted

.gitignore Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

minor: I believe, that developer related stuff (ide settings, personal tooling, etc.) should be ignored in git config --global, not in the project .gitignore. It helps maintain simple, clean, and easy to read and understand ignore file. Project .gitignore could contain some build artifacts, or dot-secrets, or smth like so.

doc.go Outdated
@@ -9,6 +9,8 @@ var ErrNotFound = errors.New("not found")
// Geche interface is a common interface for all cache implementations.
type Geche[K comparable, V any] interface {
Set(K, V)
// SetIfPresent sets the kv only if the key was already present, and returns whether the insertion was performed
SetIfPresent(K, V) bool
Copy link
Owner

Choose a reason for hiding this comment

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

Oh, I just realised that it would be a shame to implement atomic Get/Set without returning the previous value. This will increase the number of possible use cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I'll do this later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

refactored

Copy link
Owner

@C-Pro C-Pro left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@arthhhhh23
Copy link
Contributor Author

@C-Pro when you have time, could you please merge this and create a new package version? I will likely need this next week

@C-Pro C-Pro merged commit b63990f into C-Pro:main Oct 18, 2024
5 checks passed
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.

3 participants