From 11008cac8b9106d2bca0bc036ecbf110644b10c7 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sat, 29 Jun 2024 13:59:47 +0800 Subject: [PATCH] =?UTF-8?q?git=E4=BF=AE=E5=A4=8Dim=E4=BC=A0=E8=BE=93?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=B7=BB=E5=8A=A0im=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/im.go | 2 +- worker/redis.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/handler/im.go b/handler/im.go index 11ef926..9398d4e 100644 --- a/handler/im.go +++ b/handler/im.go @@ -169,7 +169,7 @@ func SRMessage(c *gin.Context) { } if msg.Type == "msg" { // 将消息发送到指定用户 - worker.PushRedisList(session+"_"+to_user_id, msg.Msg) //将消息存入redis + worker.PushRedisListWithExpire(session+"_"+to_user_id, msg.Msg, time.Hour*1) //将消息存入redis } } }(ws, res, to_user_id) diff --git a/worker/redis.go b/worker/redis.go index f4e72e9..a05fc21 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -195,6 +195,21 @@ func PushRedisList(key string, value string) bool { return true } +func PushRedisListWithExpire(key string, value string, expire time.Duration) bool { + ctx := context.Background() + err := redisClient.RPush(ctx, key, value).Err() + if err != nil { + fmt.Println("Error setting key: %v", err) + return false + } + err = redisClient.Expire(ctx, key, expire).Err() + if err != nil { + fmt.Println("Error setting key: %v", err) + return false + } + return true +} + // delete redis key func delRedis(key string) { ctx := context.Background()