diff --git a/conf/wheat-cache.yaml b/conf/wheat-cache.yaml index 981dc43..0a71f5d 100644 --- a/conf/wheat-cache.yaml +++ b/conf/wheat-cache.yaml @@ -14,8 +14,13 @@ lruCache: eventDriverSize: 2000 workTime: 1 + logPrint: stath: [ "debug", "error" ] + +middleChain: + middles:["logMiddle","mapKey"] + diff --git a/pkg/middle/middleware_driver.go b/pkg/middle/middleware_driver.go index 5c58e03..7d1d45f 100644 --- a/pkg/middle/middleware_driver.go +++ b/pkg/middle/middleware_driver.go @@ -2,9 +2,10 @@ package middle import ( "context" - "fmt" + _ "gitee.com/timedb/wheatCache/conf" "gitee.com/timedb/wheatCache/pkg/event" + "github.com/spf13/viper" ) type MiddleWare struct { @@ -28,7 +29,10 @@ func (m *MiddleWare) GetEventDriver() event.DriverInterface { } func (m *MiddleWare) work(ctx context.Context) { - getMiddlewareMap := Init() - fmt.Println(getMiddlewareMap) - + middlewareMap := Init() + event := m.eventConsumer.Receive(ctx) + middleConf := viper.GetStringSlice("middleChain.middles") + for _, middle := range middleConf { + middlewareMap[middle].Exec(event) + } } diff --git a/pkg/middle/middleware_driver_test.go b/pkg/middle/middleware_driver_test.go new file mode 100644 index 0000000..f5856f6 --- /dev/null +++ b/pkg/middle/middleware_driver_test.go @@ -0,0 +1,50 @@ +package middle + +import ( + "context" + "fmt" + "testing" + + "gitee.com/timedb/wheatCache/pkg/event" +) + +func Test_middleware_driver(t *testing.T) { + + ctx := context.Background() + type Library struct { + driver *MiddleWare + } + + library := &Library{ + driver: NewMiddleWare(), + } + + go library.driver.work(ctx) + + type A struct { + produce event.ProduceInterface + } + + a := &A{ + produce: event.NewProduce(library.driver.eventDriver), + } + + type B struct { + cumsumer event.ConsumerInterface + } + + b := &B{ + cumsumer: event.NewConsumer(library.driver.eventDriver), + } + + book := event.NewEvent("book") + book.SetMsg("title", "goland") + book.SetValue("page", 100) + + go func() { + a.produce.Call(ctx, book) + }() + + b_book := b.cumsumer.Receive(ctx) + fmt.Println(b_book.GetMsg("title")) +} diff --git a/plugins/define.go b/plugins/define.go index 37538c9..bd438d8 100644 --- a/plugins/define.go +++ b/plugins/define.go @@ -1,7 +1,9 @@ package plugins +import "gitee.com/timedb/wheatCache/pkg/event" + type MiddleToolsInterface interface { - Init() // 初始化 - Exec(interface{}) (interface{}, error) // 处理用户发送事件 - Name() string // 获取中间件名称 + Init() // 初始化 + Exec(e event.Event) (event.Event, error) // 处理用户发送事件 + Name() string // 获取中间件名称 } diff --git a/plugins/log-middle/middleware.go b/plugins/log-middle/middleware.go index c6ff102..23e8f03 100644 --- a/plugins/log-middle/middleware.go +++ b/plugins/log-middle/middleware.go @@ -1,8 +1,7 @@ package log_middle import ( - "fmt" - + "gitee.com/timedb/wheatCache/pkg/event" "gitee.com/timedb/wheatCache/plugins" ) @@ -12,9 +11,8 @@ type logMiddle struct { func (i *logMiddle) Init() { } -func (i *logMiddle) Exec(interface{}) (interface{}, error) { - fmt.Println(1) - return nil, nil +func (i *logMiddle) Exec(e event.Event) (event.Event, error) { + return e, nil } func (i *logMiddle) Name() string {