package model import ( "context" "fmt" "time" "trading-go/common" ) type Message struct { MsgType int `json:"msgType"` From uint `json:"from"` To uint `json:"to"` Time uint `json:"time"` Content any `json:"content"` } func (m Message) Save() { rds := common.Redis ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() key := fmt.Sprintf("%d:%d:%d", m.From, m.To, m.Time) rds.Set(ctx, key, m, time.Second*60*60*24*30) } func (m Message) GetFrom(uid uint) (ms []Message, err error) { rds := common.Redis ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() key := fmt.Sprintf("%d:*", uid) result, err := rds.Do(ctx, "MGET", key).Result() ms = result.([]Message) return } func (m Message) GetTo(uid uint) (ms []Message, err error) { rds := common.Redis ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() key := fmt.Sprintf("*:%d:*", uid) result, err := rds.Do(ctx, "MGET", key).Result() ms = result.([]Message) return }