thief_music_api/server/rank_data.py
Joe 3d73f1ad10
All checks were successful
continuous-integration/drone/push Build is passing
feat:新增fastapi-login,数据库表
2023-08-16 19:40:35 +08:00

30 lines
795 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
import json
import os
def get_cache_filename(rank_id):
# 生成缓存文件名以rank_id命名
# date_str = datetime.datetime.now().strftime("%Y%m%d")
return f"{rank_id}.json"
def read_cache(rank_id):
# 从缓存文件中读取数据
cache_filename = get_cache_filename(rank_id)
if os.path.exists(f"data/{cache_filename}"):
with open(f"data/{cache_filename}", "r", encoding="utf8") as file:
return json.load(file)
return None
def write_cache(rank_id, data):
# 将数据写入缓存文件
cache_filename = get_cache_filename(rank_id)
# 创建文件
if not os.path.exists("data"):
os.mkdir("data")
with open(f"data/{cache_filename}", "w", encoding="utf8") as file:
json.dump(data, file)