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

Reworked options to be variadics #143

Merged
merged 1 commit into from May 26, 2022
Merged

Reworked options to be variadics #143

merged 1 commit into from May 26, 2022

Conversation

eko
Copy link
Owner

@eko eko commented May 26, 2022

Actually, interfaces looks like this:

type CacheInterface[T any] interface {
	Get(ctx context.Context, key any) (T, error)
	Set(ctx context.Context, key any, object T, options *store.Options) error
	Delete(ctx context.Context, key any) error
	Invalidate(ctx context.Context, options *store.InvalidateOptions) error
	Clear(ctx context.Context) error
	GetType() string
}

This leads to having to pass nil as the last options argument when we do not want to have any option (which is the case very often).

In this pull request, both Cache and Store interfaces have been updated to the following:

type CacheInterface[T any] interface {
	Get(ctx context.Context, key any) (T, error)
	Set(ctx context.Context, key any, object T, options ...store.Option) error
	Delete(ctx context.Context, key any) error
	Invalidate(ctx context.Context, options ...store.InvalidateOption) error
	Clear(ctx context.Context) error
	GetType() string
}

Using variadics, it allows to have optional arguments so we've moved to the following way to pass options:

err := cacheManager.Set(
    ctx,
    "my-key",
    "my-value",
    store.WithExpiration(10 * time.Second),
    store.WithTags([]string{"my-amazing-tag"}),
)

or, when no option:

err := cacheManager.Set(ctx, "my-key", "my-value")

This pull request have also updated stores interfaces. Hard work but it was really necessary to have a better developer experience for v3 ✌️

@eko eko merged commit 2c36428 into master May 26, 2022
@eko eko deleted the better-options branch May 26, 2022 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant