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

WithKeepTTL #57

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

WithKeepTTL #57

wants to merge 1 commit into from

Conversation

RelicOfTesla
Copy link

Support KeepTTL option when update cache value.
Fix Increment/Decrement lose expiration time.

@Code-Hex
Copy link
Owner

@RelicOfTesla Hi 👋
Thanks for making a new PR!
Please write more description why do you want this option?
Why is the existing expiration option not sufficient? "Fix" but what was the problem?

@@ -82,6 +83,17 @@ func WithExpiration(exp time.Duration) ItemOption {
}
}

// WithKeepTTL is the expiration time to keep existing keys when calling Set. By default, it is replaced by the new time or never expires.
func WithKeepTTL(_keep ...bool) ItemOption {
Copy link
Owner

Choose a reason for hiding this comment

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

Is this better?

Suggested change
func WithKeepTTL(_keep ...bool) ItemOption {
func WithKeepTTL(keep bool) ItemOption {

Copy link
Author

Choose a reason for hiding this comment

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

Both are feasible, but I prefer to input less code because function names already have a certain intention

@@ -334,7 +353,7 @@ func (nc *NumberCache[K, V]) Increment(key K, n V) V {
defer nc.nmu.Unlock()
got, _ := nc.Cache.Get(key)
nv := got + n
nc.Cache.Set(key, nv)
nc.Cache.Set(key, nv, WithKeepTTL())
Copy link
Owner

Choose a reason for hiding this comment

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

We do not intend to make destructive changes to this library to ensure backward compatibility unless there is a good reason to do so. Why is this change necessary? Please provide a clear reason.

Copy link
Author

Choose a reason for hiding this comment

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

The default rules for redis/memcached and more cache libraries... We do not recommend using different designs to avoid thinking burdens

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

2 participants