feat: high-concurrency userid hashing
This commit is contained in:
8
utils/export_records.py
Normal file
8
utils/export_records.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import polars as pl
|
||||
import polars_hash as pl_hash
|
||||
|
||||
pl.scan_parquet("records.parquet").with_columns(
|
||||
pl.col("user_id").cast(pl.String).add("Lt2N5xgjJOqRsT5qVt7wWYw6SqOPZDI7")
|
||||
).with_columns(pl_hash.col("user_id").chash.sha2_256()).collect().write_parquet(
|
||||
"records_pub.parquet"
|
||||
)
|
||||
@@ -1,7 +1,8 @@
|
||||
import polars as pl
|
||||
|
||||
from helpers import salted_hash_userid
|
||||
import polars_hash as pl_hash
|
||||
|
||||
pl.scan_parquet("regions.parquet").with_columns(
|
||||
pl.col("user_id").map_elements(salted_hash_userid, return_dtype=pl.String)
|
||||
).collect().write_parquet("regions_pub.parquet")
|
||||
pl.col("user_id").cast(pl.String).add("Lt2N5xgjJOqRsT5qVt7wWYw6SqOPZDI7")
|
||||
).with_columns(pl_hash.col("user_id").chash.sha2_256()).collect().write_parquet(
|
||||
"regions_pub.parquet"
|
||||
)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user