feat: add memory cache

This commit is contained in:
zhaoyupeng
2025-07-15 18:40:13 +08:00
parent 127c57dc3a
commit 868b959c6f
7 changed files with 239 additions and 29 deletions

View File

@ -5,23 +5,24 @@ import (
"fmt"
)
type config struct {
ctx context.Context
redis *string
type option struct {
ctx context.Context
redis *string
memory bool
}
type OptionFn func(*config)
type Option func(*option)
func WithCtx(ctx context.Context) OptionFn {
return func(c *config) {
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) OptionFn {
return func(c *config) {
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)
@ -30,3 +31,9 @@ func WithRedis(host string, port int, username, password string) OptionFn {
c.redis = &uri
}
}
func WithMemory() Option {
return func(c *option) {
c.memory = true
}
}