From 43ec7f57cf82ca254491d8e84f4fa13587cc9c8f Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 8 Aug 2022 09:25:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=97=A5=E5=BF=97=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E4=BB=A5=E5=8F=8Aredis=E5=BA=93=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego/sys/log/sys_test.go | 34 ++++++++++++---- lego/sys/redis/cluster/core.go | 5 ++- lego/sys/redis/core.go | 11 +++-- lego/sys/redis/redis.go | 7 +++- lego/sys/redis/single/core.go | 5 ++- modules/redis/core.go | 10 +++++ modules/redis/expiredcomp.go | 73 +++++++++++++++++++++++++++++++++- modules/redis/module.go | 6 +++ 8 files changed, 134 insertions(+), 17 deletions(-) diff --git a/lego/sys/log/sys_test.go b/lego/sys/log/sys_test.go index 14cbfc5f7..f91b2ae22 100644 --- a/lego/sys/log/sys_test.go +++ b/lego/sys/log/sys_test.go @@ -1,18 +1,38 @@ package log_test import ( + "fmt" + "os" "testing" "go_dreamfactory/lego/sys/log" ) +type TestData struct { + Name string + Age int32 +} + +var sys log.ILog + +func TestMain(m *testing.M) { + var err error + if sys, err = log.NewSys( + log.SetFileName("./log.log"), + log.SetDebugMode(false), + log.SetLoglayer(2), + ); err != nil { + fmt.Println(err) + return + } + defer os.Exit(m.Run()) +} func Test_sys(t *testing.T) { - if err := log.OnInit(map[string]interface{}{ - "FileName": "./test.log", - "Loglevel": log.FatalLevel, - "Debugmode": true, - "Loglayer": 2, - }); err == nil { - log.Infof("测试日志接口代码!") + sys.Error("测试日志接口代码!") +} + +func Benchmark_Ability_Zap(b *testing.B) { + for i := 0; i < b.N; i++ { //use b.N for looping + sys.Error("妈妈咪呀!") } } diff --git a/lego/sys/redis/cluster/core.go b/lego/sys/redis/cluster/core.go index 4166ecea9..e27d64e36 100644 --- a/lego/sys/redis/cluster/core.go +++ b/lego/sys/redis/cluster/core.go @@ -58,10 +58,13 @@ func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd { } ///批处理 -func (this *Redis) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { +func (this *Redis) Pipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { _, err = this.client.Pipelined(ctx, fn) return } +func (this *Redis) Pipeline() redis.Pipeliner { + return this.client.Pipeline() +} ///事务 func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { diff --git a/lego/sys/redis/core.go b/lego/sys/redis/core.go index 6d100d034..e8b2a5f7c 100644 --- a/lego/sys/redis/core.go +++ b/lego/sys/redis/core.go @@ -14,7 +14,8 @@ type ( Do(ctx context.Context, args ...interface{}) *redis.Cmd Lock(key string, outTime int) (result bool, err error) UnLock(key string) (err error) - Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) + Pipeline() redis.Pipeliner + Pipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) @@ -163,9 +164,11 @@ func Context() context.Context { func Do(ctx context.Context, args ...interface{}) *redis.Cmd { return defsys.Do(ctx, args...) } - -func Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { - return defsys.Pipeline(ctx, fn) +func Pipeline() redis.Pipeliner { + return defsys.Pipeline() +} +func Pipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { + return defsys.Pipelined(ctx, fn) } func TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { return defsys.TxPipelined(ctx, fn) diff --git a/lego/sys/redis/redis.go b/lego/sys/redis/redis.go index 510a35540..fab5d5f8c 100644 --- a/lego/sys/redis/redis.go +++ b/lego/sys/redis/redis.go @@ -58,8 +58,11 @@ func (this *Redis) Context() context.Context { func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd { return this.client.Do(ctx, args...) } -func (this *Redis) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { - return this.client.Pipeline(ctx, fn) +func (this *Redis) Pipeline() redis.Pipeliner { + return this.client.Pipeline() +} +func (this *Redis) Pipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { + return this.client.Pipelined(ctx, fn) } func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { return this.client.TxPipelined(ctx, fn) diff --git a/lego/sys/redis/single/core.go b/lego/sys/redis/single/core.go index 20ebeff4c..a07e87ac9 100644 --- a/lego/sys/redis/single/core.go +++ b/lego/sys/redis/single/core.go @@ -55,10 +55,13 @@ func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd { } ///批处理 -func (this *Redis) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { +func (this *Redis) Pipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { _, err = this.client.Pipelined(ctx, fn) return } +func (this *Redis) Pipeline() redis.Pipeliner { + return this.client.Pipeline() +} ///事务 func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { diff --git a/modules/redis/core.go b/modules/redis/core.go index 65a229e12..af4e52def 100644 --- a/modules/redis/core.go +++ b/modules/redis/core.go @@ -1 +1,11 @@ package redis + +import ( + "time" +) + +type ModelDataExpired struct { + key string //主key + keys map[string]struct{} //数据集合 + expired time.Time //过期时间 +} diff --git a/modules/redis/expiredcomp.go b/modules/redis/expiredcomp.go index fb610d9fc..1df5d4a08 100644 --- a/modules/redis/expiredcomp.go +++ b/modules/redis/expiredcomp.go @@ -1,23 +1,92 @@ package redis import ( + "context" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" + "go_dreamfactory/lego/sys/redis" + "go_dreamfactory/sys/cache" + "sync" + "time" ) ///过期数据管理组件 type ExpiredComp struct { cbase.ModuleCompBase + redis redis.ISys + mu sync.RWMutex + data map[string]*ModelDataExpired } //组件初始化接口 func (this *ExpiredComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.ModuleCompBase.Init(service, module, comp, opt) - + this.redis = cache.Redis() + this.data = make(map[string]*ModelDataExpired, 1024) return } +func (this *ExpiredComp) Start() (err error) { + err = this.ModuleCompBase.Start() + go this.run() + return +} + +func (this *ExpiredComp) UpDateModel(key string, childs map[string]struct{}, expired time.Duration) { + this.mu.RLock() + exp, ok := this.data[key] + this.mu.RUnlock() + if ok { + exp.keys = childs + exp.expired = time.Now().Add(expired) + } else { + exp = &ModelDataExpired{ + key: key, + keys: childs, + expired: time.Now().Add(expired), + } + this.mu.Lock() + this.data[key] = exp + this.mu.Unlock() + } +} + //定时清理过期数据 func (this *ExpiredComp) run() { - + timer := time.NewTicker(time.Minute * 1) + defer timer.Stop() + for { + select { + case <-timer.C: + this.scanning() + break + } + } +} + +//扫描过期 +func (this *ExpiredComp) scanning() { + now := time.Now() + this.mu.Lock() + temp := make([]*ModelDataExpired, 0, len(this.data)) + for k, v := range this.data { + if v.expired.Before(now) { //过期 + temp = append(temp, v) + delete(this.data, k) + } + } + this.mu.Unlock() + ctx := context.Background() + pipe := this.redis.Pipeline() + for _, v := range temp { + pipe.Del(ctx, v.key) + if v.keys != nil { + for k1, _ := range v.keys { + pipe.Del(ctx, k1) + } + } + } + if _, err := pipe.Exec(ctx); err != nil { + + } } diff --git a/modules/redis/module.go b/modules/redis/module.go index 5ad7537b1..4be430c57 100644 --- a/modules/redis/module.go +++ b/modules/redis/module.go @@ -4,6 +4,7 @@ import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" + "time" ) /* @@ -37,3 +38,8 @@ func (this *Redis) OnInstallComp() { this.ModuleBase.OnInstallComp() this.expiredComp = this.RegisterComp(new(ExpiredComp)).(*ExpiredComp) } + +//更新缓存数据过期时间 +func (this *Redis) UpDateModel(key string, childs map[string]struct{}, expired time.Duration) { + this.expiredComp.UpDateModel(key, childs, expired) +}