From 6c902b7eb8875fe5378333f1a0344b48568f9e3c Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 21 Apr 2022 20:40:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=20refCode=20?= =?UTF-8?q?=E7=9A=84=E7=94=9F=E6=88=90=E6=96=B9=E5=BC=8F=E4=BB=A5=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=20Key=20=E5=8F=91=E7=94=9F=E7=A2=B0=E6=92=9E.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 防止 Key 发生碰撞, 使用循环检查 refCode 是否已经缓存. --- src/main/kotlin/Utils.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/Utils.kt b/src/main/kotlin/Utils.kt index 4355c26..78f4325 100644 --- a/src/main/kotlin/Utils.kt +++ b/src/main/kotlin/Utils.kt @@ -245,7 +245,10 @@ private val callbackCache: Cache = CacheBuilder. .build() fun InlineKeyboardButtonBuilder.callbackData(callback: InlineKeyboardCallback): InlineKeyboardButtonBuilder { - val cacheReferenceCode = ThreadLocalRandom.current().randomString(32) + var cacheReferenceCode: String + do { + cacheReferenceCode = Random().randomString(32) + } while (callbackCache.getIfPresent(cacheReferenceCode) != null) callbackCache.put(cacheReferenceCode, callback) callbackData("{\"rcode\":\"$cacheReferenceCode\"}") return this