diff --git a/pkg/middle/define.go b/pkg/middle/define.go index 678c75b..23c488e 100644 --- a/pkg/middle/define.go +++ b/pkg/middle/define.go @@ -1,7 +1,16 @@ package middle -import getMiddlewareMap "gitee.com/timedb/wheatCache/plugins/config" +import ( + "sync" + + getMiddlewareMap "gitee.com/timedb/wheatCache/plugins/config" +) func Init() { getMiddlewareMap.GetMiddlewareMap() } + +var ( + oneMiddle sync.Once + MiddleWareDriver *MiddleWare +) diff --git a/pkg/middle/middleware_driver.go b/pkg/middle/middleware_driver.go new file mode 100644 index 0000000..f7053a5 --- /dev/null +++ b/pkg/middle/middleware_driver.go @@ -0,0 +1,31 @@ +package middle + +import ( + "context" + + "gitee.com/timedb/wheatCache/pkg/event" +) + +type MiddleWare struct { + eventDriver event.DriverInterface + eventConsumer event.ConsumerInterface +} + +func NewMiddleWare() *MiddleWare { + oneMiddle.Do(func() { + driver := event.NewDriver(1000) + MiddleWareDriver = &MiddleWare{ + eventDriver: driver, + eventConsumer: event.NewConsumer(driver), + } + }) + return MiddleWareDriver +} + +func (m *MiddleWare) GetEventDriver() event.DriverInterface { + return m.eventDriver +} + +func (m *MiddleWare) work(ctx context.Context) { + +} diff --git a/pkg/middle/middleware_driver_test.go b/pkg/middle/middleware_driver_test.go new file mode 100644 index 0000000..3801cf2 --- /dev/null +++ b/pkg/middle/middleware_driver_test.go @@ -0,0 +1,49 @@ +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(), + } + + + 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.SetCtxValue("page", 100) + + go func() { + a.produce.Call(ctx, book) + }() + + b_book := b.cumsumer.Receive(ctx) + fmt.Println(b_book.GetMsg("title")) +}