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 custom redis prefix configuration #647

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

qqunity
Copy link

@qqunity qqunity commented Apr 19, 2023

I have supported custom redis prefix configuration for standard primitives. This is necessary when using the model when one redis instance share for many services. Because of this, collisions may occur

@qqunity
Copy link
Author

qqunity commented Apr 19, 2023

Fixes #600

@artsafin
Copy link

looks good, this is what we need in our project too

@zhifeifu
Copy link

I need this function very urgently.

@kamikazechaser
Copy link
Collaborator

How does this compare to using a different redis db (0 to 16 to n) with a different redis connection pool?

@iamjem
Copy link

iamjem commented May 3, 2023

I am not intimately familiar with this codebase, but It seems like the RDB type in internal/rdb/rdb.go is used at the heart of both the client and server implementations. Instead of making prefix configuration global, the asynq.NewClient and async.NewServer could be modified to accept a key prefix option, which in turn would get passed through to rdb.NewRDB. In the case of Server, there's already a Config type that could be used to pass the prefix. For the client, to avoid introducing a backwards breaking change, either change the function to have variadic options, or add a secondary function:

// Option 1: the variadic approach
type clientOpts struct {
    keyPrefix string
}

type ClientOption func(opts *clientOpts)

func WithClientKeyPrefix(keyPrefix string) ClientOption {
    return func(opts *clientOpts) {
        opts.keyPrefix = keyPrefix
    }
}

func NewClient(r RedisConnOpt, opts ...ClientOption) *Client {
    // ...
}

// Option 2: new function approach
type ClientConfig struct {
    KeyPrefix string
}

func NewClientWithConfig(r RedisConnOpt, config ClientConfig) *Client {
    // ...
}

Inside of RDB, anywhere that a function from internal/base is called which generates a key, wrap it in a call to r.prefix:

func (r *RDB) prefix(key string) string {
    return r.keyPrefix + key
}

Again, only recently looking at this project, not sure if this approach is bullet proof, but it would hopefully also reduce the number of files touched.

I'm assuming asynqmon would also need some sort of updates to support this change.

@sunxunkang
Copy link

这与使用不同的 Redis 数据库(0 到 16 到 n)和不同的 Redis 连接池相比如何?

redis cluster mode can not select db

@himanshuagarwal
Copy link

@hibiken Any plans on getting this done? I can help with reviewing and testing if required.

@kamikazechaser
Copy link
Collaborator

This might end up going in 0.25.1. There is already huge backlog of updates that have yet to be released.

@awanganddong
Copy link

这可能是我需要的,我是redis集群版本,无法使用db 来区分。

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

Successfully merging this pull request may close these issues.

8 participants