Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8195d633a1 | ||
|
|
4741a0bd93 | ||
|
|
f22c9df605 | ||
|
|
82cffdfe29 | ||
|
|
9da1a3bf25 | ||
|
|
2cb005e8ed | ||
|
|
374fdac477 | ||
|
|
595ababfde | ||
|
|
a62c359fd7 | ||
|
|
b45242dfeb | ||
|
|
a2763676b1 |
133
config/config.go
133
config/config.go
@@ -1,17 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/apolloconfig/agollo/v4"
|
||||
apolloConfig "github.com/apolloconfig/agollo/v4/env/config"
|
||||
"github.com/apolloconfig/agollo/v4/storage"
|
||||
"github.com/ayflying/utility_go/pkg"
|
||||
"github.com/gogf/gf/contrib/config/apollo/v2"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gres"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -31,137 +25,28 @@ type Cfg struct {
|
||||
Lock sync.Mutex
|
||||
}
|
||||
|
||||
// Deprecated : pkg.Config().GetDbFile(name)
|
||||
func (c *Cfg) GetDbFile(name string) (res *g.Var, err error) {
|
||||
get2, err := g.Model("game_config").
|
||||
Where("name", name).Master().Value("data")
|
||||
err = get2.Scan(&res)
|
||||
if res == nil {
|
||||
res = &gvar.Var{}
|
||||
}
|
||||
res, err = pkg.Config().GetDbFile(name)
|
||||
return
|
||||
}
|
||||
|
||||
// Deprecated : pkg.Config().GetFile(name, obj...)
|
||||
func (c *Cfg) GetFile(filename string, obj ...Load) (jsonObj *gjson.Json, err error) {
|
||||
pathStr := "manifest/game/"
|
||||
filePath := pathStr + filename + ".json"
|
||||
//err := gres.Load(pathStr + filename)
|
||||
|
||||
//载入静态资源到文件对象
|
||||
err = gres.Load(filePath)
|
||||
var bytes []byte
|
||||
|
||||
if gfile.IsFile(filePath) {
|
||||
bytes = gfile.GetBytes(filePath)
|
||||
} else {
|
||||
bytes = gres.GetContent(filePath)
|
||||
}
|
||||
|
||||
jsonObj, err = gjson.DecodeToJson(bytes)
|
||||
//g.Dump(filePath, jsonObj)
|
||||
jsonObj, err = pkg.Config().GetFile(filename)
|
||||
return
|
||||
}
|
||||
|
||||
// getUrlFile 获取远程配置
|
||||
// Deprecated : pkg.Config().GetUrlFile(name)
|
||||
func (c *Cfg) GetUrlFile(name string) (jsonObj *gjson.Json, err error) {
|
||||
urlStr := fmt.Sprintf("http://sdf.sdfs.sdf/%s.json", name)
|
||||
getUrl, err := g.Client().Discovery(nil).Get(nil, urlStr)
|
||||
bytes := getUrl.ReadAll()
|
||||
jsonObj, err = gjson.DecodeToJson(bytes)
|
||||
jsonObj, err = pkg.Config().GetUrlFile(name)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取阿波罗
|
||||
//func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) {
|
||||
// jsonObj, err = c.GetApolloV2(name, obj)
|
||||
// return
|
||||
//
|
||||
// //c.Lock.Lock()
|
||||
// //defer c.Lock.Unlock()
|
||||
// //
|
||||
// //Item2Obj[name+".json"] = obj
|
||||
// //var cfg = apolloConfig.AppConfig{
|
||||
// // AppID: ApolloCfg.AppID,
|
||||
// // Cluster: ApolloCfg.Cluster,
|
||||
// // IP: ApolloCfg.IP,
|
||||
// // NamespaceName: name + ".json",
|
||||
// // Secret: ApolloCfg.Secret,
|
||||
// // IsBackupConfig: ApolloCfg.IsBackupConfig,
|
||||
// // BackupConfigPath: ApolloCfg.BackupConfigPath,
|
||||
// // SyncServerTimeout: 60,
|
||||
// // MustStart: true,
|
||||
// //}
|
||||
// ////cfg.NamespaceName = name + ".json"
|
||||
// //
|
||||
// //client, err := agollo.StartWithConfig(func() (*apolloConfig.AppConfig, error) {
|
||||
// // return ApolloCfg, nil
|
||||
// //})
|
||||
// //if client == nil {
|
||||
// // return
|
||||
// //}
|
||||
// //var getStr string
|
||||
// //var getApollo *storage.Config
|
||||
// //for range 5 {
|
||||
// // getApollo = client.GetConfig(cfg.NamespaceName)
|
||||
// // if getApollo != nil {
|
||||
// // break
|
||||
// // }
|
||||
// // time.Sleep(time.Second * 5)
|
||||
// //}
|
||||
// //
|
||||
// //if getApollo != nil {
|
||||
// // getStr = getApollo.GetValue("content")
|
||||
// // if getStr != "" {
|
||||
// // //写入配置
|
||||
// // gfile.PutContents(path.Join("manifest", "game", name+".json"), getStr)
|
||||
// // }
|
||||
// //} else {
|
||||
// // jsonObj, err = c.GetFile(name)
|
||||
// //}
|
||||
// //jsonObj, err = gjson.DecodeToJson(getStr)
|
||||
// ////首次运行加入监听器
|
||||
// //if !gstr.InArray(ApolloListener, name) {
|
||||
// // c2 := &CustomChangeListener{}
|
||||
// // client.AddChangeListener(c2)
|
||||
// // ApolloListener = append(ApolloListener, name)
|
||||
// //}
|
||||
// //return
|
||||
//}
|
||||
|
||||
// Deprecated : pkg.Config().GetApollo(name, obj)
|
||||
func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) {
|
||||
Item2Obj[name+".json"] = obj
|
||||
|
||||
// 接入阿波罗配置
|
||||
ApolloCfg.NamespaceName = name + ".json"
|
||||
adapter, err := apollo.New(nil, *ApolloCfg)
|
||||
if err != nil {
|
||||
g.Log().Fatalf(nil, `%+v`, err)
|
||||
}
|
||||
// Change the adapter of default configuration instance.
|
||||
g.Cfg(name).SetAdapter(adapter)
|
||||
|
||||
//首次运行加入监听器
|
||||
if !gstr.InArray(ApolloListener, name+".json") {
|
||||
//放置监听器
|
||||
client, _ := agollo.StartWithConfig(func() (*apolloConfig.AppConfig, error) {
|
||||
return &apolloConfig.AppConfig{
|
||||
AppID: ApolloCfg.AppID,
|
||||
Cluster: ApolloCfg.Cluster,
|
||||
NamespaceName: ApolloCfg.NamespaceName,
|
||||
IP: ApolloCfg.IP,
|
||||
IsBackupConfig: ApolloCfg.IsBackupConfig,
|
||||
BackupConfigPath: ApolloCfg.BackupConfigPath,
|
||||
Secret: ApolloCfg.Secret,
|
||||
SyncServerTimeout: ApolloCfg.SyncServerTimeout,
|
||||
MustStart: ApolloCfg.MustStart,
|
||||
}, nil
|
||||
})
|
||||
c2 := &CustomChangeListener{}
|
||||
client.AddChangeListener(c2)
|
||||
ApolloListener = append(ApolloListener, name+".json")
|
||||
}
|
||||
|
||||
cfg, err := g.Cfg(name).Get(nil, "content")
|
||||
cfg.Scan(&jsonObj)
|
||||
jsonObj, err = pkg.Config().GetApollo(name, obj)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
4
go.mod
4
go.mod
@@ -4,7 +4,7 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/apolloconfig/agollo/v4 v4.4.0
|
||||
github.com/ayflying/excel2json v1.1.6
|
||||
github.com/ayflying/excel2json v1.1.9
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0
|
||||
github.com/go-pay/crypto v0.0.1
|
||||
github.com/go-pay/gopay v1.5.109
|
||||
@@ -12,7 +12,6 @@ require (
|
||||
github.com/goccy/go-json v0.10.5
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.0
|
||||
github.com/gogf/gf/v2 v2.9.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230
|
||||
github.com/minio/minio-go/v7 v7.0.91
|
||||
github.com/prometheus/client_golang v1.22.0
|
||||
@@ -45,6 +44,7 @@ require (
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@@ -50,8 +50,8 @@ github.com/apolloconfig/agollo/v4 v4.4.0/go.mod h1:6WjI68IzqMk/Y6ghMtrj5AX6Uewo2
|
||||
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
|
||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
github.com/ayflying/excel2json v1.1.6 h1:ZzsPM6QP7n2x+E91/+SSr0czSP+5szEmdI/da1EDT9k=
|
||||
github.com/ayflying/excel2json v1.1.6/go.mod h1:GjXYP2cDzeTgQxssQ3ba8QRa9JsScAeJMwCrZ7QTlQo=
|
||||
github.com/ayflying/excel2json v1.1.9 h1:AKaUFc/FNQXV/53/VSgp3J+2d4PMWhVpTDRj/yx3VOs=
|
||||
github.com/ayflying/excel2json v1.1.9/go.mod h1:GjXYP2cDzeTgQxssQ3ba8QRa9JsScAeJMwCrZ7QTlQo=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||
|
||||
@@ -136,6 +136,10 @@ func (s *sSystemCron) AddCronV2(typ v1.CronType, _func func(context.Context) err
|
||||
// @receiver s
|
||||
// @return err
|
||||
func (s *sSystemCron) StartCron() (err error) {
|
||||
//如果没有数据库配置,跳过计划任务执行
|
||||
if g.Cfg().MustGet(gctx.New(), "database") == nil {
|
||||
return
|
||||
}
|
||||
//预防重复启动
|
||||
if startTime != nil {
|
||||
return
|
||||
|
||||
5
modules/websocket/go.mod
Normal file
5
modules/websocket/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/ayflying/utility_go/modules/websocket
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require github.com/ayflying/utility_go v1.2.26
|
||||
2
modules/websocket/go.sum
Normal file
2
modules/websocket/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/ayflying/utility_go v1.2.26/go.mod h1:y6qwRUmYk73cH/dTRSzj2mnaQdtrAfFNH4wRFewxlIc=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
@@ -308,8 +308,8 @@ func (s *SocketV1) LeaveGroup(conn *WebsocketData, group string) {
|
||||
}
|
||||
|
||||
//群组广播
|
||||
func (s *SocketV1) SendGroup(cmd int32, group string, req proto.Message) {
|
||||
cacheKey := "websocket:group:" + group
|
||||
func (s *SocketV1) SendGroup(cmd int32, roomId string, req proto.Message) {
|
||||
cacheKey := "websocket:group:" + roomId
|
||||
get, _ := cache.Get(gctx.New(), cacheKey)
|
||||
var list = make(map[int64]string)
|
||||
if !get.IsNil() {
|
||||
@@ -3,121 +3,269 @@ package drive
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
fileIndex = "index.txt"
|
||||
)
|
||||
|
||||
type FileIndex struct {
|
||||
File string `json:"file"`
|
||||
Duration time.Duration `json:"duration"`
|
||||
}
|
||||
type FileData struct {
|
||||
Data interface{} `json:"data"`
|
||||
Time int64 `json:"time"`
|
||||
}
|
||||
|
||||
type AdapterFile struct {
|
||||
FilePath string
|
||||
Lock sync.Mutex
|
||||
}
|
||||
|
||||
func (a AdapterFile) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error {
|
||||
arr := strings.Split(":", gconv.String(key))
|
||||
fileName := path.Join(arr...)
|
||||
return gfile.PutBytes(fileName, gconv.Bytes(value))
|
||||
func (a *AdapterFile) Key2Name(key interface{}) string {
|
||||
md5Str, _ := gmd5.Encrypt(key)
|
||||
fileName := path.Join(md5Str[0:2], md5Str[2:4], md5Str[4:6], md5Str[16:])
|
||||
fileNameAll := path.Join(a.FilePath, fileName)
|
||||
|
||||
return fileNameAll
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) error {
|
||||
func (a *AdapterFile) AddIndex(FileName interface{}, duration time.Duration) {
|
||||
var isEdit bool
|
||||
|
||||
var setTime int64
|
||||
if duration == 0 {
|
||||
setTime = 0
|
||||
} else {
|
||||
setTime = gtime.Now().Add(duration).Unix()
|
||||
}
|
||||
saveArr := []string{
|
||||
gconv.String(FileName),
|
||||
strconv.FormatInt(setTime, 10),
|
||||
}
|
||||
saveStr := strings.Join(saveArr, "|")
|
||||
|
||||
gfile.ReadLines(fileIndex, func(text string) (err error) {
|
||||
arr := strings.Split(text, "|")
|
||||
if arr[0] == FileName {
|
||||
isEdit = true
|
||||
gfile.ReplaceFile(text, saveStr, fileIndex)
|
||||
return
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
if isEdit {
|
||||
return
|
||||
}
|
||||
|
||||
gfile.PutContentsAppend(fileIndex, saveStr+"\n")
|
||||
}
|
||||
|
||||
func (a *AdapterFile) DelIndex(FileName interface{}) {
|
||||
//var save bool
|
||||
gfile.ReadLines(fileIndex, func(text string) (err error) {
|
||||
arr := strings.Split(text, "|")
|
||||
if arr[0] == FileName {
|
||||
//save = true
|
||||
err = gfile.ReplaceFile(text+"\n", "", fileIndex)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (a *AdapterFile) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (err error) {
|
||||
fileNameAll := a.Key2Name(key)
|
||||
var send = &FileData{
|
||||
Data: value,
|
||||
Time: gtime.Now().Add(duration).Unix(),
|
||||
}
|
||||
err = gfile.PutBytes(fileNameAll, gjson.MustEncode(send))
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
a.AddIndex(key, duration)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) SetMap(ctx context.Context, data map[interface{}]interface{}, duration time.Duration) (err error) {
|
||||
for k, v := range data {
|
||||
//fileNameAll := a.Key2Name(k)
|
||||
//var send = &FileData{
|
||||
// Data: v,
|
||||
// Time: gtime.Now().Add(duration).Unix(),
|
||||
//}
|
||||
//
|
||||
//err = gfile.PutBytes(fileNameAll, gconv.Bytes(send))
|
||||
a.Set(ctx, k, v, duration)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) {
|
||||
err = a.Set(ctx, key, value, duration)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) SetIfNotExistFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetIfNotExist(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (ok bool, err error) {
|
||||
func (a *AdapterFile) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetIfNotExistFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
func (a *AdapterFile) Get(ctx context.Context, key interface{}) (res *gvar.Var, err error) {
|
||||
var data *FileData
|
||||
name := a.Key2Name(key)
|
||||
if !gfile.IsFile(name) {
|
||||
return
|
||||
}
|
||||
file := gfile.GetBytes(name)
|
||||
gjson.DecodeTo(file, &data)
|
||||
if data.Time < time.Now().Unix() {
|
||||
a.Remove(ctx, key)
|
||||
return
|
||||
}
|
||||
res = gvar.New(data.Data)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
|
||||
a.Set(ctx, key, value, duration)
|
||||
result, err = a.Get(ctx, key)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) GetOrSetFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
value := f
|
||||
a.Set(ctx, key, value, duration)
|
||||
result, err = a.Get(ctx, key)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) GetOrSetFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
a.Lock.Lock()
|
||||
defer a.Lock.Unlock()
|
||||
result, err = a.GetOrSetFunc(ctx, key, f, duration)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) Contains(ctx context.Context, key interface{}) (bool, error) {
|
||||
return gfile.IsFile(a.FilePath), nil
|
||||
}
|
||||
|
||||
func (a *AdapterFile) Size(ctx context.Context) (size int, err error) {
|
||||
size = int(gfile.Size(a.FilePath))
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) Data(ctx context.Context) (data map[interface{}]interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) SetIfNotExistFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (ok bool, err error) {
|
||||
func (a *AdapterFile) Keys(ctx context.Context) (keys []interface{}, err error) {
|
||||
gfile.ReadLines(fileIndex, func(text string) (err error) {
|
||||
|
||||
return
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) Values(ctx context.Context) (values []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Get(ctx context.Context, key interface{}) (*gvar.Var, error) {
|
||||
func (a *AdapterFile) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
|
||||
fileNameAll := a.Key2Name(key)
|
||||
getFile := gfile.GetBytes(fileNameAll)
|
||||
var data *FileData
|
||||
gconv.Scan(getFile, &data)
|
||||
oldValue.Set(data.Data)
|
||||
|
||||
var send = &FileData{
|
||||
Data: value,
|
||||
Time: data.Time,
|
||||
}
|
||||
|
||||
err = gfile.PutBytes(fileNameAll, gconv.Bytes(send))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
a.AddIndex(key, time.Duration(data.Time-gtime.Now().Unix()))
|
||||
return
|
||||
}
|
||||
|
||||
func (a *AdapterFile) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (result *gvar.Var, err error) {
|
||||
func (a *AdapterFile) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetOrSetFunc(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (a *AdapterFile) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) {
|
||||
for _, v := range keys {
|
||||
fileNameAll := a.Key2Name(v)
|
||||
lastValue, err = a.Get(ctx, fileNameAll)
|
||||
err = gfile.RemoveFile(fileNameAll)
|
||||
//删除索引文件
|
||||
a.DelIndex(v)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetOrSetFuncLock(ctx context.Context, key interface{}, f gcache.Func, duration time.Duration) (result *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (a *AdapterFile) Clear(ctx context.Context) error {
|
||||
return gfile.RemoveAll(a.FilePath)
|
||||
}
|
||||
|
||||
func (a AdapterFile) Contains(ctx context.Context, key interface{}) (bool, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Size(ctx context.Context) (size int, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Data(ctx context.Context) (data map[interface{}]interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Keys(ctx context.Context) (keys []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Values(ctx context.Context) (values []interface{}, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Update(ctx context.Context, key interface{}, value interface{}) (oldValue *gvar.Var, exist bool, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) UpdateExpire(ctx context.Context, key interface{}, duration time.Duration) (oldDuration time.Duration, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) GetExpire(ctx context.Context, key interface{}) (time.Duration, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Remove(ctx context.Context, keys ...interface{}) (lastValue *gvar.Var, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Clear(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a AdapterFile) Close(ctx context.Context) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (a *AdapterFile) Close(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewAdapterFile(filePath string) gcache.Adapter {
|
||||
return &AdapterFile{
|
||||
fileIndex = path.Join(filePath, fileIndex)
|
||||
|
||||
//清空过期文件目录
|
||||
delKeys := make([]interface{}, 0)
|
||||
gfile.ReadLines(fileIndex, func(text string) error {
|
||||
arr := strings.Split(text, "|")
|
||||
if gtime.Now().Unix() > gconv.Int64(arr[1]) {
|
||||
delKeys = append(delKeys, arr[0])
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
obj := &AdapterFile{
|
||||
FilePath: filePath,
|
||||
}
|
||||
if len(delKeys) > 0 {
|
||||
obj.Remove(gctx.New(), delKeys...)
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gres"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@@ -17,11 +18,11 @@ import (
|
||||
|
||||
var (
|
||||
//ApolloCfg *apolloConfig.AppConfig
|
||||
ApolloCfg *apollo.Config
|
||||
ApolloCfg *apollo.Config
|
||||
// ApolloListener 存储需要监听的 Apollo 配置项名称
|
||||
ApolloListener []string
|
||||
// Item2Obj 存储配置项名称和对应的加载器对象的映射
|
||||
Item2Obj = map[string]Load{}
|
||||
Item2Obj = map[string]Load{}
|
||||
)
|
||||
|
||||
// Load 接口定义了 Load 方法,用于加载数据
|
||||
@@ -86,6 +87,22 @@ func (c *Cfg) GetFile(filename string, _pathStr ...string) (jsonObj *gjson.Json,
|
||||
bytes = gres.GetContent(filePath) // 从打包资源中获取内容
|
||||
}
|
||||
|
||||
for range 5 {
|
||||
//如果还是没有读取到配置,从当前目录返回上级读取
|
||||
if bytes == nil {
|
||||
// 上级拼接完整的文件路径
|
||||
filePath = "../" + filePath
|
||||
if gfile.IsFile(filePath) {
|
||||
bytes = gfile.GetBytes(filePath) // 读取物理文件内容
|
||||
//找到配置了,跳过
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if bytes == nil {
|
||||
g.Log().Errorf(gctx.New(), "未读取到配置文件:%v", filePath)
|
||||
}
|
||||
|
||||
// 解析 JSON 内容并返回结果
|
||||
jsonObj, err = gjson.DecodeToJson(bytes)
|
||||
return
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/ayflying/utility_go/pkg/notice"
|
||||
"github.com/ayflying/utility_go/pkg/rank"
|
||||
"github.com/ayflying/utility_go/pkg/s3"
|
||||
"github.com/ayflying/utility_go/pkg/websocket"
|
||||
//"github.com/ayflying/utility_go/pkg/websocket"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
)
|
||||
|
||||
@@ -32,9 +32,9 @@ func Rank() *rank.Mod {
|
||||
return rank.New()
|
||||
}
|
||||
|
||||
func Websocket() *websocket.SocketV1 {
|
||||
return websocket.NewV1()
|
||||
}
|
||||
//func Websocket() *websocket.SocketV1 {
|
||||
// return websocket.NewV1()
|
||||
//}
|
||||
|
||||
func Config() *config.Cfg {
|
||||
return config.NewV1()
|
||||
|
||||
Reference in New Issue
Block a user