feat(middleware_driver): add middle driver

This commit is contained in:
Sodesnei 2021-10-05 20:52:01 +08:00
parent 02b048a24f
commit e08646b6a1
5 changed files with 71 additions and 12 deletions

View File

@ -14,8 +14,13 @@ lruCache:
eventDriverSize: 2000
workTime: 1
logPrint:
stath: [
"debug",
"error"
]
middleChain:
middles:["logMiddle","mapKey"]

View File

@ -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)
}
}

View File

@ -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"))
}

View File

@ -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 // 获取中间件名称
}

View File

@ -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 {