refactor: 调整 refCode 的生成方式以防止 Key 发生碰撞.

防止 Key 发生碰撞, 使用循环检查 refCode 是否已经缓存.
This commit is contained in:
LamGC 2022-04-21 20:40:33 +08:00
parent f1eef0ad94
commit 6c902b7eb8
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -245,7 +245,10 @@ private val callbackCache: Cache<String, InlineKeyboardCallback> = 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