清理代码

This commit is contained in:
dragon
2025-09-25 11:57:23 +08:00
parent 9a425a057e
commit 92c0f73020
13 changed files with 92 additions and 74 deletions

View File

@@ -0,0 +1,55 @@
package config
import (
"context"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
const TtlKeep = -1
type KVConfig interface {
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
}
func NewAutoConfig(src string) (KVConfig, error) {
if src == "" ||
strings.HasPrefix(src, "./") ||
strings.HasPrefix(src, "/") ||
strings.HasPrefix(src, "\\") ||
strings.HasPrefix(src, ".\\") {
return NewConfigMemory(src)
}
parse, err := url.Parse(src)
if err != nil {
return nil, err
}
switch parse.Scheme {
case "local":
return NewConfigMemory(parse.Path)
case "redis":
query := parse.Query()
pass := query.Get("pass")
if pass == "" {
pass = query.Get("password")
}
db := strings.TrimPrefix(parse.Path, "/")
if db == "" {
db = "0"
}
dbi, err := strconv.Atoi(db)
if err != nil {
return nil, err
}
return NewConfigRedis(parse.Host, pass, dbi)
default:
return nil, fmt.Errorf("unsupported scheme: %s", parse.Scheme)
}
}

View File

@@ -0,0 +1,106 @@
package config
import (
"context"
"encoding/json"
"os"
"path/filepath"
"sync"
"time"
"go.uber.org/zap"
)
// ConfigMemory 一个简单的内存配置归档,仅用于测试
type ConfigMemory struct {
data sync.Map
store string
}
func NewConfigMemory(store string) (KVConfig, error) {
ret := &ConfigMemory{
store: store,
data: sync.Map{},
}
if store != "" {
zap.L().Info("parse config from store", zap.String("store", store))
if err := os.MkdirAll(filepath.Dir(store), 0o755); err != nil && !os.IsExist(err) {
return nil, err
}
item := make(map[string]ConfigContent)
data, err := os.ReadFile(store)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if err == nil {
err = json.Unmarshal(data, &item)
if err != nil {
return nil, err
}
}
for key, content := range item {
if content.Ttl == nil || time.Now().Before(*content.Ttl) {
ret.data.Store(key, content)
}
}
clear(item)
}
return ret, nil
}
type ConfigContent struct {
Data string `json:"data"`
Ttl *time.Time `json:"ttl,omitempty"`
}
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 {
td = nil
}
m.data.Store(key, ConfigContent{
Data: value,
Ttl: td,
})
return nil
}
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) {
return "", os.ErrNotExist
}
return content.Data, nil
}
return "", os.ErrNotExist
}
func (m *ConfigMemory) Delete(ctx context.Context, key string) error {
m.data.Delete(key)
return nil
}
func (m *ConfigMemory) Close() error {
defer m.data.Clear()
if m.store != "" {
item := make(map[string]ConfigContent)
now := time.Now()
m.data.Range(
func(key, value interface{}) bool {
content := value.(ConfigContent)
if content.Ttl == nil || now.Before(*content.Ttl) {
item[key.(string)] = content
}
return true
})
zap.L().Debug("回写内容到本地存储", zap.String("store", m.store), zap.Int("length", len(item)))
saved, err := json.Marshal(item)
if err != nil {
return err
}
return os.WriteFile(m.store, saved, 0o600)
}
return nil
}

View File

@@ -0,0 +1,60 @@
package config
import (
"context"
"fmt"
"os"
"time"
"github.com/pkg/errors"
"github.com/valkey-io/valkey-go"
"go.uber.org/zap"
)
type ConfigRedis struct {
client valkey.Client
}
func NewConfigRedis(addr string, password string, db int) (*ConfigRedis, error) {
if addr == "" {
return nil, fmt.Errorf("addr is empty")
}
zap.L().Debug("connect redis", zap.String("addr", addr))
client, err := valkey.NewClient(valkey.ClientOption{
InitAddress: []string{addr},
Password: password,
SelectDB: db,
})
if err != nil {
return nil, err
}
return &ConfigRedis{
client: client,
}, nil
}
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(ctx, builder.Build()).Error()
}
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(ctx context.Context, key string) error {
return r.client.Do(ctx, r.client.B().Del().Key(key).Build()).Error()
}
func (r *ConfigRedis) Close() error {
r.client.Close()
return nil
}