完成缓存逻辑,有些响应数据需要根据请求参数进行区别处理,但是实际异步处理,固个别请求缓存下关键数据

This commit is contained in:
injoyai
2024-10-30 10:24:23 +08:00
parent 060cbbf301
commit 8f17e5fe5a

View File

@@ -85,6 +85,7 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
return return
} }
//从缓存中获取数据,响应数据中不同类型有不同的处理方式,但是响应无返回该类型,固根据消息id进行缓存
val, _ := this.m.GetAndDel(conv.String(f.MsgID)) val, _ := this.m.GetAndDel(conv.String(f.MsgID))
var resp any var resp any
@@ -130,8 +131,11 @@ func (this *Client) handlerDealMessage(c *client.Client, msg ios.Acker) {
} }
// SendFrame 发送数据,并等待响应 // SendFrame 发送数据,并等待响应
func (this *Client) SendFrame(f *protocol.Frame) (any, error) { func (this *Client) SendFrame(f *protocol.Frame, cache ...any) (any, error) {
f.MsgID = atomic.AddUint32(&this.msgID, 1) f.MsgID = atomic.AddUint32(&this.msgID, 1)
if len(cache) > 0 {
this.m.Set(conv.String(f.MsgID), cache[0])
}
if _, err := this.Client.Write(f.Bytes()); err != nil { if _, err := this.Client.Write(f.Bytes()); err != nil {
return nil, err return nil, err
} }
@@ -212,7 +216,7 @@ func (this *Client) GetStockMinuteTrade(exchange protocol.Exchange, code string,
if err != nil { if err != nil {
return nil, err return nil, err
} }
result, err := this.SendFrame(f) result, err := this.SendFrame(f, code)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -244,7 +248,7 @@ func (this *Client) GetStockHistoryMinuteTrade(t time.Time, exchange protocol.Ex
if err != nil { if err != nil {
return nil, err return nil, err
} }
result, err := this.SendFrame(f) result, err := this.SendFrame(f, code)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -275,7 +279,7 @@ func (this *Client) GetStockKline(Type protocol.TypeKline, req *protocol.StockKl
if err != nil { if err != nil {
return nil, err return nil, err
} }
result, err := this.SendFrame(f) result, err := this.SendFrame(f, Type.Uint16())
if err != nil { if err != nil {
return nil, err return nil, err
} }