feat: 添加 database - cache(redis)

This commit is contained in:
zhaoyupeng
2025-06-18 18:44:45 +08:00
parent 7dd48c0c50
commit edce9fe73f
8 changed files with 475 additions and 9 deletions

32
database/cache/option.go vendored Normal file
View File

@ -0,0 +1,32 @@
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("%s:%d", host, port)
if username != "" || password != "" {
uri = fmt.Sprintf("redis://%s:%s@%s:%d", username, password, host, port)
}
c.redis = &uri
}
}