为相关函数补充 context

This commit is contained in:
dragon
2025-08-18 16:28:13 +08:00
parent a385414e3f
commit d74e356975
10 changed files with 81 additions and 77 deletions

View File

@@ -13,9 +13,9 @@ import (
const TtlKeep = -1
type KVConfig interface {
Put(key string, value string, ttl time.Duration) error
Get(key string) (string, error)
Delete(key string) error
Put(ctx context.Context, key string, value string, ttl time.Duration) error
Get(ctx context.Context, key string) (string, error)
Delete(ctx context.Context, key string) error
io.Closer
}
@@ -48,7 +48,7 @@ func NewAutoConfig(src string) (KVConfig, error) {
if err != nil {
return nil, err
}
return NewConfigRedis(context.Background(), parse.Host, pass, dbi)
return NewConfigRedis(parse.Host, pass, dbi)
default:
return nil, fmt.Errorf("unsupported scheme: %s", parse.Scheme)
}

View File

@@ -1,6 +1,7 @@
package utils
import (
"context"
"encoding/json"
"os"
"path/filepath"
@@ -52,7 +53,7 @@ type ConfigContent struct {
Ttl *time.Time `json:"ttl,omitempty"`
}
func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error {
func (m *ConfigMemory) Put(ctx context.Context, key string, value string, ttl time.Duration) error {
d := time.Now().Add(ttl)
td := &d
if ttl == -1 {
@@ -65,7 +66,7 @@ func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error {
return nil
}
func (m *ConfigMemory) Get(key string) (string, error) {
func (m *ConfigMemory) Get(ctx context.Context, key string) (string, error) {
if value, ok := m.data.Load(key); ok {
content := value.(ConfigContent)
if content.Ttl != nil && time.Now().After(*content.Ttl) {
@@ -76,7 +77,7 @@ func (m *ConfigMemory) Get(key string) (string, error) {
return "", os.ErrNotExist
}
func (m *ConfigMemory) Delete(key string) error {
func (m *ConfigMemory) Delete(ctx context.Context, key string) error {
m.data.Delete(key)
return nil
}

View File

@@ -13,11 +13,10 @@ import (
)
type ConfigRedis struct {
ctx context.Context
client valkey.Client
}
func NewConfigRedis(ctx context.Context, addr string, password string, db int) (*ConfigRedis, error) {
func NewConfigRedis(addr string, password string, db int) (*ConfigRedis, error) {
if addr == "" {
return nil, fmt.Errorf("addr is empty")
}
@@ -31,33 +30,28 @@ func NewConfigRedis(ctx context.Context, addr string, password string, db int) (
return nil, err
}
return &ConfigRedis{
ctx: ctx,
client: client,
}, nil
}
func (r *ConfigRedis) Put(key string, value string, ttl time.Duration) error {
func (r *ConfigRedis) Put(ctx context.Context, key string, value string, ttl time.Duration) error {
builder := r.client.B().Set().Key(key).Value(value)
if ttl != TtlKeep {
builder.Ex(ttl)
}
return r.client.Do(r.ctx, builder.Build()).Error()
return r.client.Do(ctx, builder.Build()).Error()
}
func (r *ConfigRedis) Get(key string) (string, error) {
v, err := r.client.Do(r.ctx, r.client.B().Get().Key(key).Build()).ToString()
func (r *ConfigRedis) Get(ctx context.Context, key string) (string, error) {
v, err := r.client.Do(ctx, r.client.B().Get().Key(key).Build()).ToString()
if err != nil && errors.Is(err, valkey.Nil) {
return "", os.ErrNotExist
}
return v, err
}
func (r *ConfigRedis) Delete(key string) error {
err := r.client.Do(r.ctx, r.client.B().Del().Key(key).Build()).Error()
if err != nil && errors.Is(err, valkey.Nil) {
return os.ErrNotExist
}
return nil
func (r *ConfigRedis) Delete(ctx context.Context, key string) error {
return r.client.Do(ctx, r.client.B().Del().Key(key).Build()).Error()
}
func (r *ConfigRedis) Close() error {