Files
packages/database/cache/option.go
2025-07-15 18:40:13 +08:00

40 lines
631 B
Go

package cache
import (
"context"
"fmt"
)
type option struct {
ctx context.Context
redis *string
memory bool
}
type Option func(*option)
func WithCtx(ctx context.Context) Option {
return func(c *option) {
if ctx != nil {
c.ctx = ctx
}
}
}
func WithRedis(host string, port int, username, password string) Option {
return func(c *option) {
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
}
}
func WithMemory() Option {
return func(c *option) {
c.memory = true
}
}