From d856e3c5707343e5db9f3e61869cc8d4755f4a37 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 20 Apr 2026 08:26:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(lua):=20=E6=94=B9=E8=BF=9B=20dictReplace=20?= =?UTF-8?q?key=20=E5=AD=98=E5=9C=A8=E6=80=A7=E6=A3=80=E6=9F=A5=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复 shared_dict.replace() 方法对过期 key 的判断: - 区分 key 不存在和 key 存在但已过期的情况 - Get() 返回 val="" 且 expired=false 表示 key 不存在 - 先检查不存在情况,再检查过期情况 Co-Authored-By: Claude Opus 4.7 --- internal/lua/api_shared_dict.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/lua/api_shared_dict.go b/internal/lua/api_shared_dict.go index b239a0a..1226c4b 100644 --- a/internal/lua/api_shared_dict.go +++ b/internal/lua/api_shared_dict.go @@ -278,9 +278,20 @@ func dictReplace(L *glua.LState) int { ttl = time.Duration(L.CheckNumber(4)) * time.Second } - // 检查是否存在 - _, expired, _ := dict.Get(key) + // 检查是否存在(Get 返回: value, expired, error) + // expired=true 表示存在但已过期,expired=false 表示不存在或存在且未过期 + // 需要区分不存在和存在的情况 + val, expired, _ := dict.Get(key) + // 如果 val 为空且 expired 为 false,表示 key 不存在 + // 如果 expired 为 true,表示 key 存在但已过期(也算不存在) + if val == "" && !expired { + // key 不存在 + L.Push(glua.LFalse) + L.Push(glua.LString("not found")) + return 2 + } if expired { + // key 存在但已过期,也算不存在 L.Push(glua.LFalse) L.Push(glua.LString("not found")) return 2