feat: high-concurrency userid hashing

This commit is contained in:
mokurin000
2025-08-11 12:12:14 +08:00
parent 1d2e3fc7cc
commit b0942e2af4
4 changed files with 26 additions and 5 deletions

View File

@@ -2,15 +2,25 @@ from decimal import Decimal, getcontext
import hashlib
import orjson as json
from diskcache import Cache
getcontext().prec = 28
CACHE = Cache("target")
def salted_hash_userid(user_id: int):
hex = CACHE.get(user_id)
if hex is not None:
return hex
SALT = b"Lt2N5xgjJOqRsT5qVt7wWYw6SqOPZDI7"
hash_uid = hashlib.sha256(f"{user_id}".encode("utf-8") + SALT)
return hash_uid.hexdigest()[:16]
result = hash_uid.hexdigest()[:16]
CACHE.add(user_id, result)
return result
def dx_rating(difficulty: Decimal, achievement: int) -> int: