From f760b7a73f5efaae54c4e2d96615eb2f0d95b967 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Thu, 27 Jun 2024 16:38:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dim=E6=B6=88=E6=81=AF=E8=8E=B7?= =?UTF-8?q?=E5=8F=96key=EF=BC=8C=E5=88=A0=E9=99=A4redis=20queue=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/im.go | 12 ++++++++---- worker/redis.go | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/handler/im.go b/handler/im.go index bda9e8e..5938f8b 100644 --- a/handler/im.go +++ b/handler/im.go @@ -66,13 +66,17 @@ func GetImKey(c *gin.Context) { return } else { retrievedData["is_read"] = 1 - res2 := worker.SetRedisWithExpire(redis_key+"_connection", retrievedData["im_session"].(string), time.Second*30) - if res2 == false { + str, _ := json.Marshal(retrievedData) + res3 := worker.SetRedisWithExpire(redis_key, string(str), time.Second*300) + res2 := worker.SetRedisWithExpire(redis_key+"_connection", retrievedData["im_session"].(string), time.Second*300) + if res2 == false || res3 == false { c.JSON(http.StatusOK, gin.H{"error": "set key failed", "code": proto.OperationFailed, "message": "failed"}) return } c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": retrievedData, "message": "success"}) return + c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "data": retrievedData, "message": "success"}) + return } } @@ -92,7 +96,7 @@ func GetImKey(c *gin.Context) { data["is_read"] = 0 str, _ := json.Marshal(data) //将key存入redis - res := worker.SetRedisWithExpire(redis_key, string(str), time.Second*30) + res := worker.SetRedisWithExpire(redis_key, string(str), time.Second*300) if res == false { c.JSON(http.StatusOK, gin.H{"error": "set key failed", "code": proto.OperationFailed, "message": "failed"}) return @@ -176,7 +180,7 @@ func SRMessage(c *gin.Context) { break } } - time.Sleep(time.Microsecond * 100) // 每100毫秒查询一次 + time.Sleep(time.Second * 1) // 每100毫秒查询一次 } else { clientsMux.Lock() delete(clients, ws) diff --git a/worker/redis.go b/worker/redis.go index 4d5bd37..cb011e0 100644 --- a/worker/redis.go +++ b/worker/redis.go @@ -171,7 +171,6 @@ func PopRedisListLeft(key string) string { ctx := context.Background() val, err := redisClient.LPop(ctx, key).Result() // 从 Redis 读取键值, 如果键不存在则返回空字符串, 如果出现错误则返回错误 if err != nil { - fmt.Println(key, " Error reading from Redis: %v", err) return "" } return val