支持存储配置到 redis
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const TtlKeep = -1
|
||||
@@ -19,92 +19,32 @@ type KVConfig interface {
|
||||
io.Closer
|
||||
}
|
||||
|
||||
// ConfigMemory 一个简单的内存配置归档,仅用于测试
|
||||
type ConfigMemory struct {
|
||||
data sync.Map
|
||||
store string
|
||||
}
|
||||
|
||||
func NewConfigMemory(store string) (KVConfig, error) {
|
||||
ret := &ConfigMemory{
|
||||
store: store,
|
||||
data: sync.Map{},
|
||||
func NewAutoConfig(src string) (KVConfig, error) {
|
||||
if src == "" ||
|
||||
strings.HasPrefix(src, "./") ||
|
||||
strings.HasPrefix(src, "/") ||
|
||||
strings.HasPrefix(src, "\\") ||
|
||||
strings.HasPrefix(src, ".\\") {
|
||||
return NewConfigMemory(src)
|
||||
}
|
||||
if store != "" {
|
||||
item := make(map[string]ConfigContent)
|
||||
data, err := os.ReadFile(store)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
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()
|
||||
addr := query.Get("addr")
|
||||
pass := query.Get("pass")
|
||||
db := query.Get("db")
|
||||
dbi, err := strconv.Atoi(db)
|
||||
if err != nil {
|
||||
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 NewConfigRedis(context.Background(), addr, pass, dbi)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported scheme: %s", parse.Scheme)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
type ConfigContent struct {
|
||||
Data string `json:"data"`
|
||||
Ttl *time.Time `json:"ttl,omitempty"`
|
||||
}
|
||||
|
||||
func (m *ConfigMemory) Put(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(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(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
|
||||
}
|
||||
|
||||
105
pkg/utils/config_memory.go
Normal file
105
pkg/utils/config_memory.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"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(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(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(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
|
||||
}
|
||||
44
pkg/utils/config_redis.go
Normal file
44
pkg/utils/config_redis.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user