Merge branch 'dev' of http://git.legu.cc/liwei_3d/go_dreamfactory into meixiongfeng
This commit is contained in:
commit
b9817ee6d5
@ -1,18 +1,38 @@
|
|||||||
package log_test
|
package log_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go_dreamfactory/lego/sys/log"
|
"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) {
|
func Test_sys(t *testing.T) {
|
||||||
if err := log.OnInit(map[string]interface{}{
|
sys.Error("测试日志接口代码!")
|
||||||
"FileName": "./test.log",
|
}
|
||||||
"Loglevel": log.FatalLevel,
|
|
||||||
"Debugmode": true,
|
func Benchmark_Ability_Zap(b *testing.B) {
|
||||||
"Loglayer": 2,
|
for i := 0; i < b.N; i++ { //use b.N for looping
|
||||||
}); err == nil {
|
sys.Error("妈妈咪呀!")
|
||||||
log.Infof("测试日志接口代码!")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
_, err = this.client.Pipelined(ctx, fn)
|
||||||
return
|
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) {
|
func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) {
|
||||||
|
@ -14,7 +14,8 @@ type (
|
|||||||
Do(ctx context.Context, args ...interface{}) *redis.Cmd
|
Do(ctx context.Context, args ...interface{}) *redis.Cmd
|
||||||
Lock(key string, outTime int) (result bool, err error)
|
Lock(key string, outTime int) (result bool, err error)
|
||||||
UnLock(key string) (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)
|
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)
|
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 {
|
func Do(ctx context.Context, args ...interface{}) *redis.Cmd {
|
||||||
return defsys.Do(ctx, args...)
|
return defsys.Do(ctx, args...)
|
||||||
}
|
}
|
||||||
|
func Pipeline() redis.Pipeliner {
|
||||||
func Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) {
|
return defsys.Pipeline()
|
||||||
return defsys.Pipeline(ctx, fn)
|
}
|
||||||
|
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) {
|
func TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) {
|
||||||
return defsys.TxPipelined(ctx, fn)
|
return defsys.TxPipelined(ctx, fn)
|
||||||
|
@ -58,8 +58,11 @@ func (this *Redis) Context() context.Context {
|
|||||||
func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd {
|
func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd {
|
||||||
return this.client.Do(ctx, args...)
|
return this.client.Do(ctx, args...)
|
||||||
}
|
}
|
||||||
func (this *Redis) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) {
|
func (this *Redis) Pipeline() redis.Pipeliner {
|
||||||
return this.client.Pipeline(ctx, fn)
|
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) {
|
func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) {
|
||||||
return this.client.TxPipelined(ctx, fn)
|
return this.client.TxPipelined(ctx, fn)
|
||||||
|
@ -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)
|
_, err = this.client.Pipelined(ctx, fn)
|
||||||
return
|
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) {
|
func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) {
|
||||||
|
@ -1 +1,11 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ModelDataExpired struct {
|
||||||
|
key string //主key
|
||||||
|
keys map[string]struct{} //数据集合
|
||||||
|
expired time.Time //过期时间
|
||||||
|
}
|
||||||
|
@ -1,23 +1,92 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"go_dreamfactory/lego/core"
|
"go_dreamfactory/lego/core"
|
||||||
"go_dreamfactory/lego/core/cbase"
|
"go_dreamfactory/lego/core/cbase"
|
||||||
|
"go_dreamfactory/lego/sys/redis"
|
||||||
|
"go_dreamfactory/sys/cache"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
///过期数据管理组件
|
///过期数据管理组件
|
||||||
type ExpiredComp struct {
|
type ExpiredComp struct {
|
||||||
cbase.ModuleCompBase
|
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) {
|
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.ModuleCompBase.Init(service, module, comp, opt)
|
||||||
|
this.redis = cache.Redis()
|
||||||
|
this.data = make(map[string]*ModelDataExpired, 1024)
|
||||||
return
|
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() {
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"go_dreamfactory/comm"
|
"go_dreamfactory/comm"
|
||||||
"go_dreamfactory/lego/core"
|
"go_dreamfactory/lego/core"
|
||||||
"go_dreamfactory/modules"
|
"go_dreamfactory/modules"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -37,3 +38,8 @@ func (this *Redis) OnInstallComp() {
|
|||||||
this.ModuleBase.OnInstallComp()
|
this.ModuleBase.OnInstallComp()
|
||||||
this.expiredComp = this.RegisterComp(new(ExpiredComp)).(*ExpiredComp)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user