Merge branch 'cache-middle-zlj'

This commit is contained in:
Sodesnei 2021-09-19 18:11:43 +08:00
parent e742267b68
commit d3c3c2bfe1
5 changed files with 96 additions and 36 deletions

24
pkg/middle/Data.go Normal file
View File

@ -0,0 +1,24 @@
package middle
// 数据类型
type data struct {
m map[string]interface{}
}
// 数据初始化,分配内存
func NewData() *data {
return &data{
make(map[string]interface{}),
}
}
// 获取数据
func (d *data) Get(key string) interface{} {
return d.m[key]
}
// 推入数据
func (d *data) Put(key string, val interface{}) error {
d.m[key] = val
return nil
}

View File

@ -1,36 +1,11 @@
package middle
import "fmt"
// 获取数据的接口
type Msg interface {
Put(key string, val interface{}) error
Get(key string) interface{}
}
// 数据类型
type data struct {
m map[string]interface{}
}
// 数据初始化,分配内存
func NewData() *data {
return &data{
make(map[string]interface{}),
}
}
// 获取数据
func (d *data) Get(key string) interface{} {
return d.m[key]
}
// 推入数据
func (d *data) Put(key string, val interface{}) error {
d.m[key] = val
return nil
}
type middleWare interface {
Put(msg Msg)
Out() Msg
@ -49,6 +24,7 @@ func NewDriver() *Driver {
return new(Driver)
}
// 驱动
func (d *Driver) Start(msg Msg) {
for _, m := range d.conf {
@ -56,9 +32,9 @@ func (d *Driver) Start(msg Msg) {
go m.Middle.Put(msg)
msg = m.Middle.Out()
fmt.Println(msg.Get("1"))
}
}
func main() {
@ -67,6 +43,14 @@ func main() {
Weight: 1,
Middle: NewUpload(),
},
{
Weight: 2,
Middle: NewStorage(),
},
{
Weight: 3,
Middle: NewSend(),
},
}
da := NewDriver()

24
pkg/middle/Send.go Normal file
View File

@ -0,0 +1,24 @@
package middle
import "fmt"
type Send struct {
ch chan *data
}
func NewSend() *Send {
return &Send{
make(chan *data),
}
}
func (m *Send) Put(msg Msg) {
da := NewData()
da.Put("1", msg)
fmt.Println("传入Send")
m.ch <- da
}
func (m *Send) Out() Msg {
return <-m.ch
}

24
pkg/middle/Storage.go Normal file
View File

@ -0,0 +1,24 @@
package middle
import "fmt"
type Storage struct {
ch chan *data
}
func NewStorage() *Storage {
return &Storage{
make(chan *data),
}
}
func (m *Storage) Put(msg Msg) {
da := NewData()
da.Put("1", msg)
fmt.Println("传入Storage")
m.ch <- da
}
func (m *Storage) Out() Msg {
return <-m.ch
}

View File

@ -1,21 +1,25 @@
package middle
import "fmt"
type Upload struct {
ch chan *data
}
func (m *Upload) Put(msg Msg) {
da := NewData()
da.Put("1", msg)
m.ch <- da
}
func (m *Upload) Out() Msg {
return <-m.ch
}
func NewUpload() *Upload {
return &Upload{
make(chan *data),
}
}
func (m *Upload) Put(msg Msg) {
da := NewData()
da.Put("1", msg)
fmt.Println("传入Upload")
m.ch <- da
}
func (m *Upload) Out() Msg {
return <-m.ch
}