支持存储配置到 redis

This commit is contained in:
dragon
2025-07-23 11:24:47 +08:00
parent 12eb21a5cd
commit 1e0fa15c42
7 changed files with 202 additions and 106 deletions

44
pkg/utils/config_redis.go Normal file
View File

@@ -0,0 +1,44 @@
package utils
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
type ConfigRedis struct {
ctx context.Context
client *redis.Client
}
func NewConfigRedis(ctx context.Context, addr string, password string, db int) (*ConfigRedis, error) {
if addr == "" {
return nil, fmt.Errorf("addr is empty")
}
return &ConfigRedis{
ctx: ctx,
client: redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
}),
}, nil
}
func (r *ConfigRedis) Put(key string, value string, ttl time.Duration) error {
return r.client.Set(r.ctx, key, value, ttl).Err()
}
func (r *ConfigRedis) Get(key string) (string, error) {
return r.client.Get(r.ctx, key).Result()
}
func (r *ConfigRedis) Delete(key string) error {
return r.client.Del(r.ctx, key).Err()
}
func (r *ConfigRedis) Close() error {
return r.client.Close()
}