33 lines
545 B
Go
33 lines
545 B
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
type config struct {
|
|
ctx context.Context
|
|
redis *string
|
|
}
|
|
|
|
type OptionFn func(*config)
|
|
|
|
func WithCtx(ctx context.Context) OptionFn {
|
|
return func(c *config) {
|
|
if ctx != nil {
|
|
c.ctx = ctx
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithRedis(host string, port int, username, password string) OptionFn {
|
|
return func(c *config) {
|
|
uri := fmt.Sprintf("redis://%s:%d", host, port)
|
|
if username != "" || password != "" {
|
|
uri = fmt.Sprintf("redis://%s:%s@%s:%d", username, password, host, port)
|
|
}
|
|
|
|
c.redis = &uri
|
|
}
|
|
}
|